handlebars.js: relative paths in partials not working [duplicate]

独自空忆成欢 提交于 2019-12-08 03:41:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25808922/handlebars-js-relative-paths-in-partials-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!