最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If statement in jQuery template - Stack Overflow

programmeradmin1浏览0评论

The following code returns an error "Uncaught ReferenceError: size is not defined" in Chrome if variable size is not defined:

<script type="text/x-jquery-tmpl">    
    {{if name && size}}
        <p>${name}</p>
        <p>${size}</p>
    {{/if}}
</script>

While this code works fine :

<script type="text/x-jquery-tmpl">  
    {{if name}}
        {{if size}}
            <p>${name}</p>
            <p>${size}</p>
        {{/if}}
    {{/if}}
</script>

Can I somehow make it work in Chrome without using double if statement and why does it return an error at all?

The following code returns an error "Uncaught ReferenceError: size is not defined" in Chrome if variable size is not defined:

<script type="text/x-jquery-tmpl">    
    {{if name && size}}
        <p>${name}</p>
        <p>${size}</p>
    {{/if}}
</script>

While this code works fine :

<script type="text/x-jquery-tmpl">  
    {{if name}}
        {{if size}}
            <p>${name}</p>
            <p>${size}</p>
        {{/if}}
    {{/if}}
</script>

Can I somehow make it work in Chrome without using double if statement and why does it return an error at all?

Share Improve this question asked Sep 11, 2012 at 9:29 bogatyrjovbogatyrjov 5,3789 gold badges38 silver badges61 bronze badges 3
  • Have you tried {{ if (name && size) }}? – alexbusu Commented Sep 11, 2012 at 9:32
  • Post your object, maybe the object doesn't contain size or size is in other level than with name. – Snake Eyes Commented Sep 11, 2012 at 9:32
  • Alexander, yes, i've tried. Snake Eyes, the object doesn't contain size. The if statement is there to check it. – bogatyrjov Commented Sep 11, 2012 at 13:00
Add a ment  | 

2 Answers 2

Reset to default 4

try this:

<script type="text/x-jquery-tmpl">    
    {{if name && size != null && size}}
        <p>${name}</p>
        <p>${size}</p>
    {{/if}}
</script>

Try this

<script type="text/x-jquery-tmpl">    
    {{if (name != null && size != null)}}
        <p>${name}</p>
        <p>${size}</p>
    {{/if}}
</script>

Note the space after if statement.

发布评论

评论列表(0)

  1. 暂无评论