Jquery handlebar.js(如果未按预期工作)
精神错乱检查,是否有人确认mainLocation应返回“是”。出于某种原因,它返回“否” 这是我的密码Jquery handlebar.js(如果未按预期工作),jquery,handlebars.js,Jquery,Handlebars.js,精神错乱检查,是否有人确认mainLocation应返回“是”。出于某种原因,它返回“否” 这是我的密码 <script id="Templates" type="text/x-handlebars-template"> <div id="RowExpand{{id}}" class="collapse"> {{#if mainLocation}} yes - Some content here... {{else}} no - Some cont
<script id="Templates" type="text/x-handlebars-template">
<div id="RowExpand{{id}}" class="collapse">
{{#if mainLocation}}
yes - Some content here...
{{else}}
no - Some content here...
{{/if}}
</div>
</script>
此模板的数据上下文是什么 要实现此功能,可以使用“带块辅助对象”将该部分包装到
In your html file:
<head>
...
</head>
<body>
{{> example}}
<body>
<template name="example">
{{#with data}}
{{#if mainLocation}}
html code
{{else}}
other html code
{{/if}}
{{/with}}
</template>
In your js file:
Template.example.helpers({
data: function () {
return {mainLocation: true};
}
});
html文件中的:
...
{{>示例}
{{{有数据}
{{{#if mainLocation}
html代码
{{else}
其他html代码
{{/if}
{{/与}}
在js文件中:
Template.example.helpers({
数据:函数(){
返回{mainLocation:true};
}
});
以防其他人有相同的问题。这就是解决我问题的方法。我创建了一个自定义的把手助手,如下所示
Handlebars.registerHelper("ismainlocation", function(mainlocation) {
if(mainlocation){
return "Yes";
}
else{
return "No";
}
});
我改变了我的脚本模板
<script id="Templates" type="text/x-handlebars-template">
<div id="RowExpand{{id}}" class="collapse">
{{ismainlocation mainlocation}}
</div>
</script>
{{ismainlocation mainlocation}
感谢您的回复。我对车把相当陌生,如果我把你的脚本放在正确的位置,我就不知道了。这就是我尝试过的:{{{{with data}}{{{{if mainLocation}}html{{else}其他html{{/if}}{{/with}模板。示例。helpers({data:function(){return{mainLocation:true};});但它不起作用。我做错了什么?如果你正在从事流星项目,谢谢你。您不需要脚本标记。我要修改我的答案,让你们知道我没有用流星。
<script id="Templates" type="text/x-handlebars-template">
<div id="RowExpand{{id}}" class="collapse">
{{ismainlocation mainlocation}}
</div>
</script>