骨干替代语法

时间:2013-03-28 18:14:01

标签: backbone.js syntax

我来自php,我可以使用替代语法if else语句,如

<?php if(): ?>
    // conditional html goes here
<?php endif;?>

我的问题是,是否有办法在骨干视图上做同样的事情,如

<% if(condition): %>
    html code goes here ;

<% endif; %>

2 个答案:

答案 0 :(得分:1)

如果您使用的是默认Underscore templates,那么:

<% if(condition) { %>
    HTML goes here
<% } %>

不要忘记{},否则你会感到很乱。

演示:http://jsfiddle.net/ambiguous/W33Tw/

答案 1 :(得分:0)

这个问题应该用您使用骨干网的任何模板解决方案进行标记。如果你使用的是把手(很棒http://handlebarsjs.com/),你可以用:

{{#if contextPropertyThatEvaluatesSanelyAsABoolean}}
  your html!
{{else}}
  different html!
{{/if}}

然后传入类似的上下文:

$(@el).html(this.template({
  contextPropertyThatEvaluatesSanelyAsABoolean: true
}))

或者如果您使用的是coffeescript:

$(@el).html @template
  contextPropertyThatEvaluatesSanelyAsABoolean: true

其他模板语言也有类似的解决方案。