2012-06-12 52 views
0

mustache.js是否支持这样的表情? - >{{类型== “一”}}mustache.js是否支持这样的表情判断?

我该怎么办?

胡子:

{{#items}} 
    {{type == "a"}} 
    <li>{{name}} {{type}}</li> 
    {{/link}} 
    {{type == "b"}} 
    <strong>{{name}} {{type}}</strong> 
    {{type == "c"}} 
    <span>{{name}} {{type}}</span> 
    {{/link}} 
{{/items}} 

JSON:

{ 
    "header": "Colors", 
    "items": [ 
     {"name": "red", "type": "a", "url": "#Red"}, 
     {"name": "green", "type": "b", "url": "#Green"}, 
     {"name": "blue", "type": "c", "url": "#Blue"} 
    ], 
    "empty": false 
} 

想输出:

<li>red a</li> 
<strong>green b</strong> 
<span>blue c</span> 

回答

0

你不能因为胡须是逻辑较少。

最好的做法是改变你的输入JSON,使'逻辑'已经包含在输入中。喜欢的东西:

{ 
    "header": "Colors", 
    "items": [ 
     {"a": {"name": "red", "url": "#Red"}}, 
     {"b": {"name": "green", "url": "#Green"}}, 
     {"c": {"name": "blue", "url": "#Blue"}} 
    ], 
    "empty": false 
} 

然后:

{{#items}} 
    {{#a}} 
    <li>{{name}} {{type}}</li> 
    {{/a}} 
    {{#b}} 
    <strong>{{name}} {{type}}</strong> 
    {{/b}} 
    {{#c}} 
    <span>{{name}} {{type}}</span> 
    {{/c}} 
{{/items}} 

会产生你想要的东西。

编辑 不知何故,我无法得到上面的例子工作。我使用的是Hogan(由Twitter维护的胡子版本,速度更快,并且可以使用Hogan做一些额外的事情,请务必检查一下),但我确信没有任何东西要处理上述事情应该正常工作的事实。

无论如何,我可以得到以下工作在提供的演示页面,它与原始示例保持接近,并完成您想要的输出。一般说明:您可以检查是否存在字段(例如,使用{{#isa}}),但标记中不能包含任何其他逻辑。

{{#items}} 
    {{#isa}} 
    <li><strong>{{name}}</strong></li> 
    {{/isa}} 
    {{#isb}} 
    <li><a href="{{url}}">{{name}}</a></li> 
    {{/isb}} 
    {{#isc}} 
    <li><span href="{{url}}">{{name}}</span ></li> 
    {{/isc}} 
{{/items}} 

JSON

{ 
    "header": "Colors", 
    "items": [ 
     {"name": "red", "isa": true, "url": "#Red"}, 
     {"name": "green", "isb": true, "url": "#Green"}, 
     {"name": "blue", "isc": true, "url": "#Blue"} 
    ], 
    "empty": false 
} 

心连心

+0

感谢,你知道什么模板引擎的支持表达判断呢?我不明白为什么“小胡子”做出的逻辑少,不方便使用,有什么好处呢? –

+0

至于为什么:http://stackoverflow.com/questions/3896730/whats-the-advantage-of-logic-less-template-such-as-mustache ..话虽如此,即使没有逻辑小胡子是真正强大,当你来使用它。例如,在你的json中有一个函数被Mustache中的引用调用是完全合法的。现在,如果该功能是对json中的其他数据进行处理,那么您很快就会意识到它的效果很好。 –

+0

谢谢,根据你的说法,我在这里尝试一下:http://mustache.github.com/#demo为什么** {{name}} {{type}} **不输出任何内容? –

相关问题