metalsmith系列'路径'无法从Handlebars模板访问键

metalsmith-collections 'path' key not accessible from Handlebars template?

本文关键字:访问 Handlebars 系列 路径 metalsmith      更新时间:2023-11-11

我的模板:

{{#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和其他所有密钥。提前感谢您的帮助。

谢谢——在发布我的问题时,我意识到我试图在永久链接有机会将该属性添加到文件树之前访问"path"键——只需将永久链接移动到模板上方就解决了这个问题。

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