[英]Handlebars define a {{with}} in condition
I want to show a different content from my json in my template depending of a javascript defined variable. 我想根据javascript定义的变量在模板中显示与json不同的内容。
Here is the JSON file : 这是JSON文件:
{
"normal" :{
"content":"content"
}
"expert" :{
"content":"content"
}
[HTML] Now in my template, I have this : [HTML]现在在我的模板中,有这个:
{{#with normal}}
<p>{{content}}</p>
{{/with}}
Now I want to be able to use 'with normal', or 'with expert' depending of a javascript-side defined variable. 现在,我希望能够根据JavaScript端定义的变量使用“ with normal”或“ with expert”。
I have made a compare helper but, I have to use the code twice and that's ugly. 我做了一个比较助手,但是,我不得不两次使用代码,这很丑陋。 So Maybe someone know how to do it properly ? 那么,也许有人知道如何正确地做到这一点? Thanks. 谢谢。
Do you really need logic in your template? 您是否确实需要模板中的逻辑?
If the body of the {{#with}}
block is identical for data of normal
or expert
types, then you can probably get rid of the with
entirely and pass the appropriate block into the template. 如果{{#with}}
块的主体对于normal
或expert
类型的数据相同,则您可以完全删除with
并将适当的块传递到模板中。 This pushes the logic back into the script (and hopefully the view, if you have an MVC framework), which is good, and optimizes out a level of the template (also good, especially since that is a with
, which tend to be slow). 这样可以将逻辑推回到脚本中(如果有MVC框架,则可以将视图推回到视图中),这很好,并且可以优化模板的级别(也很好,尤其是因为with
,这往往很慢) )。
Note that Handlebars is perfectly happy taking an object with more data than it needs, so writing a template that depends on the normal
block and passing the expert
with additional details (presumably) will cause no problems at all. 请注意,Handlebars非常乐意获取具有所需数据量更多的数据,因此编写依赖于normal
块的模板并向expert
传递其他详细信息(大概)不会造成任何问题。
您可以使用{{#if和else...。此外,还可以使处理程序成为全局处理程序并使用两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.