本文介绍了从Handlebars模板无法获取Metalsmith-collections'路径'密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模板:

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

渲染(this.path is undefined):

 < span class =Category__Title>法语< / span> 
< a href =>红烧蔬菜< / a>
< span class =Category__Title>泰国< / span>
< a href =>米粉< / a>

我正在使用metalsmith:

<$ p $(b)使用(markdown())
.use(模板({
引擎:'把手',
目录:'templates'
}))
.use(永久链接({
pattern:':title'
}))
。目的地('./ public')

在编译时, p>

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

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

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

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

解决方案

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

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


My template:

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

Renders ( this.path is undefined ):

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

I'm using metalsmith:

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

At the time of compiling, I console log to collection

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

And I can see each collection has an array of files, and each file DOES contain the key 'path'. Console log ->

 { 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' }

Strange? I can programmatically access file.path through node. Additionally, Handlebars is able to access file.title and every other key. Thanks in advance for the help.

解决方案

Thanks -- in posting my question I realized I was attempting to access the 'path' key before permalinks had a chance to add that property to the file tree -- simply moving permalinks above templates solved this issue.

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

这篇关于从Handlebars模板无法获取Metalsmith-collections'路径'密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:02