【问题标题】:Is there a way to give Ghost static pages access to the 'posts' variable that index.hbs is passed?有没有办法让 Ghost 静态页面访问 index.hbs 传递的“posts”变量?
【发布时间】:2014-05-13 19:51:13
【问题描述】:

我希望使用Ghost 来托管博客和静态网站,因此结构可能如下所示:

  • /:登陆页面(不是博客登陆页面,不需要需要访问posts)
  • /blog/:博客登录页面(需要访问 posts,index.hbs 通常可以访问)
  • /page1/ 等:静态页面将根据需要使用 page.hbs 或 page-page1.hbs
  • /blog-post-whatever/ 等:将使用 post.hbs 的博客文章

我预见到的唯一问题是只有 index.hbs(据我所知)传递了posts模板变量(请参阅code on GitHub here)。

在我提交拉取请求之前,很高兴知道:

  1. 是否有现有方法可以访问page.hbs 中的posts 变量?
  2. 如果没有,是否值得为此提交拉取请求?
  3. 如果是,我们真的想将posts 发送到所有页面吗?还是应该将拉取请求分开page.hbs 并仅将其发送给那些?还是有更好的方法来做到这一点?

【问题讨论】:

    标签: node.js ghost-blog


    【解决方案1】:

    如果您不介意破解 Ghost 核心文件,那么您可以在当前版本的 Ghost (0.7.4) 中进行破解。如果升级到新的 Ghost 版本,则需要重新破解此 hack。

    首先创建模板文件(升级后不会改变):

    在以下位置创建主页模板:

    contents/themes/theme-name/home.hbs

    home.hbs 现在取代 index.hbs 并将代替它进行渲染。

    同时在以下位置创建博客模板文件:

    contents/themes/theme-name/blog.hbs

    添加分页帖子的把手元素是

    {{> "loop"}}
    

    所以这应该在 blog.hbs 文件中。

    同样,如果您升级到新版本的 Ghost,上述文件不会更改。

    现在编辑core/server目录中的以下文件:

    我在您需要添加的代码段前后添加了几行代码,以便您更轻松地找到需要添加新代码的位置。

    /core/server/routes/frontend.js:

    之前:

    indexRouter.route('/').get(frontend.index);
    indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.index);
    

    之后:

    indexRouter.route('/').get(frontend.index);
    indexRouter.route('/blog/').get(frontend.blog);
    indexRouter.route('/' + routeKeywords.page + '/:page/').get(frontend.index);
    

    这调用了前端控制器,它将以与“索引”和“主页”相同的数据级别呈现博客页面(默认是加载最近帖子的第一页),从而使我们能够使用“循环”在 /blog/ 页面中。

    /core/server/controllers/frontend/index.js

    之前:

    frontendControllers = {
        index: renderChannel('index'),
        tag: renderChannel('tag'),
    

    之后:

    frontendControllers = {
        index: renderChannel('index'),
        blog: renderChannel('blog'),
        tag: renderChannel('tag'),
    

    /core/server/controllers/frontend/channel-config.js

    之前:

    getConfig = function getConfig(name) {
        var defaults = {
            index: {
                name: 'index',
                route: '/',
                frontPageTemplate: 'home'
            },
            tag: {
    

    之后:

    getConfig = function getConfig(name) {
        var defaults = {
            index: {
                name: 'index',
                route: '/',
                frontPageTemplate: 'home'
            },
            blog: {
                name: 'blog',
                route: '/blog/',
                frontPageTemplate: 'blog'
            },
            tag: {
    

    /core/server/controllers/frontend/channel-config.js 之前:

    indexPattern = new RegExp('^\\/' + config.routeKeywords.page + '\\/'),
    rssPattern = new RegExp('^\\/rss\\/'),
    homePattern = new RegExp('^\\/$');
    

    之后:

    indexPattern = new RegExp('^\\/' + config.routeKeywords.page + '\\/'),
    rssPattern = new RegExp('^\\/rss\\/'),
    blogPattern = new RegExp('^\\/blog\\/'),
    homePattern = new RegExp('^\\/$');
    

    和

    之前:

     if (indexPattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('index');
        } else if (homePattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('home');
            res.locals.context.push('index');
        } else if (rssPattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('rss');
        } else if (privatePattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('private');
    

    之后:

     if (indexPattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('index');
        } else if (homePattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('home');
            res.locals.context.push('index');
        } else if (blogPattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('blog');
        } else if (rssPattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('rss');
        } else if (privatePattern.test(res.locals.relativeUrl)) {
            res.locals.context.push('private');
    

    重新启动服务器,您应该会看到新的 /blog/ 页面出现最近的博客文章列表

    【讨论】:

      【解决方案2】:

      这是我目前正在使用的解决方案。我有一个画布外导航,我想用它来显示指向我最新的posts 的链接。在主页上,这很好用:我遍历posts 并呈现一些链接。在其他页面上,我没有可用的 posts 变量。

      我的解决方案是:将主页上的相关帖子链接包装在一个 id 为“posts”的 div 中,然后我对该特定内容发出 ajax 请求(使用 jQuery 的负载)并将其注入到我的导航中主页以外的其他页面。这是jQuery's load docs的链接。

      代码:

      index.hbs

      <div id='posts'>
        {{#foreach posts}}
          <li>
            <a href="{{url}}">{{{title}}}</a>
          </li>
        {{/foreach}}
      </div>
      

      app.js

      var $latest = $('#posts');
      
      if ( location.pathname !== '/' )
        $latest.load('/ #posts li');
      

      【讨论】:

        【解决方案3】:

        目前(Ghost v0.5.8)无法在页面模板中访问posts。

        我认为它可能不值得提交拉取请求。 Ghost 开发人员似乎对此有自己的计划,并一直表示他们会使用此功能。希望它很快,因为它是基本功能。

        解决这个问题的最佳方法是自己破解核心。最终,更好的方法是使用钩子。看起来 Ghost API 最终会开放到您可以连接到插件的核心功能的地步,就像 Wordpress 做的一样。 https://github.com/TryGhost/Ghost/wiki/Apps-Getting-Started-for-Ghost-Devs

        如果这是其他人将使用的主题,我建议在 Ghost 的当前限制范围内工作。我知道这非常烦人,但从长远来看,这对您的用户和您的声誉是最好的。

        如果这仅适合您,那么我会破解核心以在每条路线中将帖子或页面列表公开为本地人。如果您熟悉 Express,那么这应该不是很困难。

        我认为你的做法很有创意,我的一部分人喜欢它,但它确实是一个非常丑陋的 hack。如果您发现自己经常将这些解决方案组合在一起,那么 Ghost 可能不是您想要使用的工具。

        【讨论】:

          【解决方案4】:

          比 briangonzalez 更好的解决方案是从 RSS 提要而不是主页获取帖子信息。

          请参阅this gist 了解如何完成。

          【讨论】:

            【解决方案5】:

            现在您可以使用 ghost-url-api,它目前处于测试阶段,但您可以在管理中激活它(设置 > 实验室)。

            例如,{{#get}} 帮助器可以像这样在静态页面中使用:

            {{#get "posts" limit="3" include="author,tags"}}
             {{#foreach posts}}
                ... call the loop
             {{/foreach}}
            {{/get}}
            

            更多信息: http://themes.ghost.org/docs/ghost-url-api

            【讨论】:

              【解决方案6】:

              从 Ghost v0.9.0 开始,Channels API 仍在开发中。但是,现在实现这一点要简单得多。它仍然需要修改核心文件,但我计划很快提交一些拉取请求。目前,以下方法的一个缺点是您的sitemap-pages.xml 将不包含/blog/ URL。

              感谢@Yuval's answer 为我们揭开序幕。


              为您的索引页面创建一个模板文件,路径为content/themes/theme-name/index.hbs。这可以包含您想要的“静态”主页的任何内容。


              为您的博客索引页面创建一个模板文件,路径为content/themes/theme-name/blog.hbs。这只需要包含:

              {{> "loop"}}
              

              在/core/server/controllers/frontend/channel-config.js:

              编辑var defaults 对象以包含:

              blog: {
                name: 'blog',
                route: '/blog/'
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-03-18
                • 2012-08-08
                • 1970-01-01
                • 2021-02-07
                • 2023-04-10
                相关资源
                最近更新 更多