有没有办法让Ghost静态页面访问index.hbs传递的'posts'variables?

我正在寻找使用Ghost来托pipe博客和静态网站,所以结构可能是这样的:

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

我预见到的唯一问题是只有 index.hbs (据我所知)被传递给posts模板variables(参见GitHub上的代码 )。

在我提交拉请求之前,先知道下面的内容是很好的:

  1. 是否有一种现有的方法来访问page.hbspostsvariables?
  2. 如果没有,是否值得为此提交一个请求?
  3. 如果是的话,我们是否真的想发posts所有的页面? 或者拉请求拆分page.hbs ,只发送给那些? 还是有更好的方法来做到这一点?

这是我目前使用的解决scheme。 我有一个canvas导航,我想用来显示链接到我的最新posts 。 在主页上,这个工程很好:我遍历posts和呈现一些链接。 在其他页面上,我没有可以使用的postsvariables。

我的解决scheme是这样的:将主页上的相关post链接包装在一个id为“posts”的div中,然后对该特定内容做一个ajax请求(使用jQuery的加载),并将其注入到我的导航中,除了所有其他页面主页。 这里是jQuery负载文档的链接。

码:

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'); 

如果您不介意攻击Ghost核心文件,那么您可以在当前版本的Ghost(0.7.4)中执行此操作。 如果升级到新的Ghost版本,这个黑客将需要重新创build。

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

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

内容/主题/主题名称/ home.hbs

home.hbs现在替代index.hbs ,将被呈现,而不是它。

在以下位置创build博客模板文件:

内容/主题/主题名称/ blog.hbs

添加分页的post的句柄元素是

 {{> "loop"}} 

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

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

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

我在你需要添加的代码段前后添加了几行,这样你就可以更容易地find需要添加新代码的位置。

/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); 

这将调用前端控制器,它将呈现与“index”和“home”相同的数据级别的博客页面(默认情况下载入最近发布的第一页),从而使我们能够使用/博客/页面。

/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'); 

重新启动服务器,你应该看到新的/博客/页面列出了最近的博客文章

目前没有办法(Ghost v0.5.8)访问页面模板中的posts

我认为它可能不值得提交拉请求。 鬼魂开发者似乎有自己的计划,并不断说他们会绕过这个function。 希望很快,因为它是基本的function。

最好的办法就是自己破解核心。 最终,更好的方式来做到这一点将是一个钩子。 它看起来像Ghost API最终会打开,你可以挂钩到核心function的插件几乎相同的方式Wordpress。 https://github.com/TryGhost/Ghost/wiki/Apps-Getting-Started-for-Ghost-Devs

如果这是他人将使用的主题,我会build议在当前Ghost的限制内工作。 我知道,这是非常烦人的,但从长远来看,它最适合您的用户和您的声誉。

如果这只是为了你,那么我会破解核心,在每个路线中显示一个post或页面列表作为当地人。 如果你熟悉Express,那么这应该不是很困难。

我认为你做这件事的方式非常有创意,而且我喜欢它的一部分,但它确实是一个严重丑陋的黑客攻击。 如果你发现自己把这些解决scheme拼凑在一起,那么Ghost可能不是你想要使用的工具。

比briangonzalez更好的解决scheme是从RSS提要中获取posts-info,而不是主页。

看到这个要点如何完成。

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

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

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

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

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

感谢@ Yuval的解答 。


使用pathcontent/themes/theme-name/index.hbs创build索引页面的模板文件。 这可以包含任何你想要的“静态”主页。


使用pathcontent/themes/theme-name/blog.hbs为您的博客索引页创build一个模板文件。 这只需要包含:

 {{> "loop"}} 

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

编辑var defaults对象以包含:

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