If statement in jQuery template

bogatyrjov picture bogatyrjov · Sep 11, 2012 · Viewed 12.2k times · Source

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?

Answer

Mathlight picture Mathlight · Sep 11, 2012

try this:

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