【问题标题】:Context of {{title}} in master template when using multiple data/json files using assemble使用 assemble 使用多个数据/json 文件时,主模板中 {{title}} 的上下文
【发布时间】:2014-07-23 09:21:14
【问题描述】:

我将 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>

我有 2 个用于数据的 json 文件:

foo1.json

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

foo2.json

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

我有 2 页:

foo1.hbs

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

foo2.hbs

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

我的 gruntfile.js sn-p:

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 返回标题
  • 我也试过 {{this.page.title}} 但同样两个结果都从 foo1.json 返回标题。当assemble编译页面时,它是否使用所有concat所有json文件并将它们用于每个页面?我以为page和json如果同名的话是一对一的关系?
  • 你确定{{page.title}}?你的例子对我来说是正确的。
  • 如果我在不同的文件夹中有另一个 index.hbs 显然它不起作用(见下文)

标签: handlebars.js assemble


【解决方案1】:

@Adi 我设置了一个 repo here,其中包含您描述的结构。

我刚刚更改了this code in 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>

如果您有我们可以查看的 repo,它可能有助于追查问题。

希望这会有所帮助。

【讨论】:

  • 您可以在此处找到 repo:github.com/adrianjacob/assemble-546 如果您查看 src/index.html 和 src/insurance/index.html,您会发现它们在运行“grunt assemble”后共享相同的标题。但是,如果您查看 json 文件,它们应该有不同的标题。干杯,A。
  • 现在,assemble 只使用文件的基本名称来进行匹配。所以第二个index 文件会覆盖第一个index 文件中的数据。此外,您的保险文件夹中有foo.json,而不是index.json。尝试将insurance/index.hbs 更改为insurance/foo.hbs,您应该会看到标题发生了变化。
  • 如果你想渲染一个 index.html 和一个 insurance/index.html,你会怎么做?是否也有基于文件夹进行匹配的计划?
  • 我用 Grunt 选项解决了这个问题: buildPages: { files: [ { expand: true, cwd: '/', src: ['pages/**/ *.hbs'], dest: '/' } ] }
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2018-08-18
相关资源
最近更新 更多