使用assemble时,当使用多个数据/JSON文件时,在主模板中{{title}}的上下文是什么?

9

我将使用assemble.io来制作一个简单的静态网站,但是{{title}}标签存在问题。以下是该问题的大致概述。

这是我的layout.hbs文件:

<!DOCTYPE html>
<html>
  <head>
    <title>{{title}}</title>
  </head>
  <body>
    <!-- the body tag is used to "pull in" content from pages -->
    {{> body }}
  </body>
</html>

I have 2 json files for data:

foo1.json

{
  "title": "Hello world I am title 1"
}

foo2.json

{
  "title": "I am a different title"
}

And I have 2 pages:

foo1.hbs

{{#foo1 }} 
 {{> module1 }}
 {{> module2 }}
 {{> module3 }}
{{/foo1 }}

foo2.hbs

{{#foo2 }} 
 {{> module1 }}
 {{> module2 }}
 {{> module3 }}
{{/foo2 }}

以下是我的 gruntfile.js 代码片段:

options: {
  layout: "src/responsive/layouts/layout.hbs",
  partials: 'src/responsive/modules/**/*.hbs',
  data: 'src/responsive/data/**/*.json',
  flatten: false
},
pages: {
  expand: true,
  cwd: 'src/responsive/pages',
  src: '**/*.hbs',
  dest: 'src/'
}

当我运行'grunt assemble'时,没有页面标题。我认为这与上下文有关,如果我将layout.hbs中的{{title}}更改为{{foo1.title}}{{foo2.title}},它可以工作,但是由于它们共享此模板,因此两个页面都会得到相同的标题。
如何使layout.hbs中的{{title}}上下文适用于传递的所有json文件?

我已经尝试过{{page.title}},但两者都返回了foo1.json的标题。 - Adi
我也尝试过{{this.page.title}},但两个结果都返回了foo1.json的标题。当组装编译页面时,它是否使用所有连接的json文件并将它们用于每个页面?如果它们具有相同的名称,我认为页面和json之间存在一对一的关系? - Adi
1
你确定 {{page.title}} 吗?使用你的示例,它对我来说是正确的。 - raidendev
显然,如果我在另一个文件夹中有另一个index.hbs文件,则它无法正常工作(请参见下文)。 - Adi
1个回答

3

@Adi 我在这里设置了一个包含你所描述的结构的repo

我只是更改了layout.hbs中的这段代码,它正如预期的那样工作。

<!DOCTYPE html>
<html>
  <head>
    <title>{{page.title}}</title>
  </head>
  <body>
    <!-- the body tag is used to "pull in" content from pages -->
    {{> body }}
  </body>
</html>

如果您有一个仓库可以供我们查看,这可能有助于追踪问题。
希望这有所帮助。

您可以在这里找到该存储库: https://github.com/adrianjacob/assemble-546如果您查看 src/index.html 和 src/insurance/index.html,您会发现在运行“grunt assemble”后它们共享相同的标题。然而,如果您查看 JSON 文件,它们应该有不同的标题。谢谢, A. - Adi
现在,assemble只使用文件的basename进行匹配。因此,第二个“index”文件将覆盖第一个“index”文件中的数据。此外,您在保险文件夹中有“foo.json”,而不是“index.json”。尝试将“insurance/index.hbs”更改为“insurance/foo.hbs”,您应该会看到标题更改。 - doowb
如果您想呈现一个index.html和insurance/index.html,您会怎么做?是否有计划基于文件夹进行匹配? - Adi
我使用Grunt选项解决了这个问题:buildPages: { files: [ { expand: true, cwd: '<%= site.templates %>/', src: ['pages/**/*.hbs'], dest: '<%= site.dest %>/' } ] } - mummybot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接