metalsmith-collections 'path' 键无法从 Handlebars 模板访问?

2024-04-12

我的模板:

{{#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 和所有其他键。先谢谢您的帮助。


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

.use(permalinks({
    pattern: ':title',
    relative: false
  }))
  .use(templates({
    engine: 'handlebars',
    directory: 'templates'
  }))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

metalsmith-collections 'path' 键无法从 Handlebars 模板访问? 的相关文章

随机推荐