Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 把手模板内模板_Javascript_Mustache_Handlebars.js - Fatal编程技术网

Javascript 把手模板内模板

Javascript 把手模板内模板,javascript,mustache,handlebars.js,Javascript,Mustache,Handlebars.js,如何在车把中的模板内创建模板? 这些是我的模板: {{content}} 我是一只猫 我是一条狗 我想在运行时将适当的模板(猫模板或狗模板)加载到动物模板中。如何执行此操作?您可以根据呈现的项目计算templateName的项目视图属性: App.AnimalView=Ember.View.extend({ templateNameBinding:函数(){ var kind=this.get('animal.kind'); 返回“%@-template”.fmt(种类); }.proper

如何在车把中的模板内创建模板?
这些是我的模板:


{{content}}
我是一只猫
我是一条狗

我想在运行时将适当的模板(猫模板或狗模板)加载到动物模板中。如何执行此操作?

您可以根据呈现的项目计算
templateName
的项目视图属性:

App.AnimalView=Ember.View.extend({
templateNameBinding:函数(){
var kind=this.get('animal.kind');
返回“%@-template”.fmt(种类);
}.property('animal.kind'))
});
然后在容器模板中,通过其视图渲染项目:


{{#对于内容中的动物}
{{view App.AnimalView animalBinding=“animal”}
{{/for}}

当您有一大块handlebar.js模板需要在几个不同的上下文中使用时,部分内容就很方便了

<script id="people-template" type="text/x-handlebars-template">
  {{#each people}}
    {{> person}}
  {{/each}}
</script>

<script id="person-partial" type="text/x-handlebars-template">
  <div class="person">
    <h2>{{first_name}} {{last_name}}</h2>
    <div class="phone">{{phone}}</div>
    <div class="email"><a href="mailto:{{email}}">{{email}}</a></div>
    <div class="since">User since {{member_since}}</div>
  </div>
</script>

<script type="text/javascript">
  $(document).ready(function() {
    var template = Handlebars.compile($("#people-template").html());
    Handlebars.registerPartial("person", $("#person-partial").html());

    template(yourData);
  }
</script>

{{{每个人}
{{>人}
{{/每个}}
{{first_name}{{last_name}}
{{phone}}
用户自{{member_-since}}
$(文档).ready(函数(){
var template=handlebar.compile($(“#人员模板”).html();
Handlebar.registerPartial(“person”,$(“#person partial”).html();
模板(数据);
}

事实上,我正在寻找一种不依赖于Ember的解决方案,因为我是为jquery插件制作的。在这种情况下,您不应该将您的问题标记为…:-)我认为Ember.js用户可能知道如何在没有Ember的情况下解决它。:)我将删除余烬标签。我们通常不会在这里提供太多的链接“仅链接”答案,但您提供的不仅仅是链接。也许这是关于“最苗条”的有效答案,仅供参考。欢迎