【问题标题】:metalsmith-collections 'path' key not accessible from Handlebars template?无法从 Handlebars 模板访问 metalsmith-collections 'path' 键?
【发布时间】:2015-02-16 05:00:07
【问题描述】:

我的模板:

{{#each collections }}
<span class="Category__Title">{{ @key }}</span>
  {{#each this }}
    <a href="{{ this.path }}">{{ this.title }}</a>
  {{/each}}
{{/each}}

渲染(this.path 未定义):

<span class="Category__Title">French</span>
    <a href="">Braised vegetables</a>
<span class="Category__Title">Thai</span>
    <a href="">Rice noodles</a>

我正在使用金属匠:

 metalsmith
  .use(collections())
  .use(markdown())
  .use(templates({
    engine: 'handlebars',
    directory: 'templates'
  }))
  .use(permalinks({
    pattern: ':title'
  }))
  .destination('./public')

在编译的时候,我控制台日志到集合

var m = metalsmith.metadata();
console.log(m.collections);

我可以看到每个集合都有一个文件数组,每个文件都包含键“路径”。控制台日志 ->

 { title: 'Braised vegetables',
  date: '10/12/1923',
  tags: [ 'braise', 'old world' ],
  collection: [ 'french' ],
  template: 'recipe.hbt',
  contents: <Buffer 3...>,
  mode: '0644',
  stats: { },
  path: 'women-s-liberation-1906' }

奇怪?我可以通过节点以编程方式访问 file.path。此外,Handlebars 能够访问 file.title 和所有其他键。在此先感谢您的帮助。

【问题讨论】:

    标签: javascript handlebars.js metalsmith


    【解决方案1】:

    谢谢——在发布我的问题时,我意识到我试图在永久链接有机会将该属性添加到文件树之前访问“路径”键——只需将永久链接移动到模板上方即可解决此问题。

    .use(permalinks({
        pattern: ':title',
        relative: false
      }))
      .use(templates({
        engine: 'handlebars',
        directory: 'templates'
      }))
    

    【讨论】:

    • 太棒了!我花了过去三个小时寻找解决方案@.@ 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-11-24
    • 2014-04-12
    • 2017-06-15
    • 2012-07-10
    • 2016-12-24
    • 2012-12-02
    • 2013-10-23
    • 1970-01-01
    相关资源
    最近更新 更多