3

私のテンプレート:

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

レンダリング ( this.path は undefined ):

<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 およびその他すべてのキーにアクセスできます。助けてくれてありがとう。

4

1 に答える 1

6

ありがとう - 質問を投稿する際に、パーマリンクがそのプロパティをファイル ツリーに追加する前に、「パス」キーにアクセスしようとしていたことに気付きました - テンプレートの上にパーマリンクを移動するだけで、この問題は解決しました。

.use(permalinks({
    pattern: ':title',
    relative: false
  }))
  .use(templates({
    engine: 'handlebars',
    directory: 'templates'
  }))
于 2015-02-16T05:04:17.680 に答える