handlebars.js:partials中的相对路径不起作用 [英] handlebars.js: relative paths in partials not working
问题描述
我用部分文件替换了文件中的一些重复代码,但现在相关部分的调用不再有效。
我有这样的数据结构:
var data = {staff:[
{name:Alan},
{ name:Bettina}
],company:Rad,Inc.};
原始模板如下所示:
< script id =first_templatetype =text / x-handlebars-template>
{{#each staff}}
< li>名称:{{name}},公司:{{../company}}</li>
{{/ each}}
< / script>
我像这样改变它以使用部分内容:
< script id =list-partialtype =text / x-handlebars-template>
< li>名称:{{name}},公司:{{../company}}</li>
< / script>
< script id =second_templatetype =text / x-handlebars-template>
{{#each staff}}
{{> list}}
{{/ each}}
< / script>
在部分公司名称不会呈现的示例中。我在jsfiddle上做了一个工作示例: http://jsfiddle.net/staeff/qwms6h2b/ <有人知道,为什么它不工作,我正在尝试做什么?
由raidendev在评论中回复
问题出在{{../company}}结构中,因为您拥有没有上部../在部分水平。解决这个问题的方法之一是将#each移到partial中,并将其命名为staffList。但正确的方法是重构你的数据结构。 - raidendev
好的,我发现,我的问题已经在stackoverflow上讨论和解决,但我不知道,寻找。查看问题处理栏 - 是否可以访问部分中的父上下文?解决我的确切问题。
I replaced some repeating code in my file with a partials, but now the call of an relative part is not working anymore.
I have this data-structure:
var data = { staff: [
{"name": "Alan"},
{"name": "Bettina"}
],"company": "Rad, Inc."};
The original template looks like this:
<script id="first_template" type="text/x-handlebars-template">
{{#each staff}}
<li>Name: {{name}} , Company: {{../company}}</li>
{{/each}}
</script>
I changed it like this to use a partial:
<script id="list-partial" type="text/x-handlebars-template">
<li>Name: {{name}} , Company: {{../company}}</li>
</script>
<script id="second_template" type="text/x-handlebars-template">
{{#each staff}}
{{> list}}
{{/each}}
</script>
In the example with the partial the company name does not get rendered. I put a working example on jsfiddle: http://jsfiddle.net/staeff/qwms6h2b/
Does anyone see, why it is not working, what I am trying to do?
Answered by raidendev in the comments
The problem is in {{../company}} construction, because you have no upper ../ level in the partial. One of the ways to solve this is to move #each inside the partial and name it something like "staffList". But the right way is to refactor your data structure. – raidendev
Ok, I found out, that my question has been discussed and solved on stackoverflow, but I didn't know, what to search for. See question handlebars - is it possible to access parent context in a partial? for solutions to my exact problem.
这篇关于handlebars.js:partials中的相对路径不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!