车把局部背景

ale*_*ias 2 javascript jquery templates handlebars.js

我有一个数组,其中包含社交按钮(href,alt,img)的信息.我创建了一个部分循环数组并添加对象,这就是我所拥有的.

数组:

var social=[
    {

        "url":"http://twitter.com/share?text="+encodeURIComponent(this.title)+" "+encodeURIComponent('#grnsrve')+"&url="+domain+"&via="+twitterAcct,
        "image":"IMG/media/twitter_16.png",
        "alt":"twitter link"
    },
    {
        "url":"http://twitter.com/share?text="+encodeURIComponent(this.title)+" "+encodeURIComponent('#grnsrve')+"&url="+domain+"&via="+twitterAcct,
        "image":"IMG/media/twitter_16.png",
        "alt":"twitter link"
    },
    {
        "url":"http://twitter.com/share?text="+encodeURIComponent(this.title)+" "+encodeURIComponent('#grnsrve')+"&url="+domain+"&via="+twitterAcct,
        "image":"IMG/media/twitter_16.png",
        "alt":"twitter link"
    }
];
Run Code Online (Sandbox Code Playgroud)

模板:

social_partial = '<a href="{{url}}" alt="{{alt}}"><img src="{{image}}"/></a>';
Run Code Online (Sandbox Code Playgroud)

部分功能:

Handlebars.registerPartial('addSocial',social_partial);
Run Code Online (Sandbox Code Playgroud)

和主要模板:

<div class="tip_social">
    {{>addSocial social}}
</div>
Run Code Online (Sandbox Code Playgroud)

我得到'Depth0是未定义的错误'.我尝试寻找有关获得不同背景的parials的文档,但我还没有找到它.

编辑 这里是一个更完整的小提琴

Pau*_*aul 7

同样的问题对我来说.稍后挖掘和一个面掌,这是我发现的.

tl;博士回答

非常小心你传递给你的部分内容.如果以某种方式将null或空对象传递给部分,则会depth0 is undefined出现错误

答案非常长

JSFiddle示例:

在工作版本中唯一改变的是我将有效的上下文传递给partial.

BTW一个非常有用的技巧是使用这个博客的调试助手

Answerbot独裁者"代码示例"

<script id="parent">
 <table>
   <thead>
      <tr>
        <th>One</th>
        <th>Two</th>
      </tr>
    </thead>
    <tbody>
    {{#each collection}}
       {{>myPartial nothingHere}} <!-- Broken context -->
    {{/each}}
    </tbody>
 </table>
</script>
Run Code Online (Sandbox Code Playgroud)

上面破碎的上下文会导致把手死亡.对于你的问题,尝试用{{debug}}标签挖掘它,看看是否有帮助.

有关更好的代码示例,请查看jsFiddles.将所有代码重新发布在这里,将其格式化以使其看起来非常漂亮并使得StackOverflow的回答独特的dictator很高兴在工作中做到这一点^ _~