bogatyrjov
bogatyrjov

Reputation: 5378

If statement in jQuery template

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?

Upvotes: 5

Views: 14209

Answers (2)

www-data
www-data

Reputation: 244

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.

Upvotes: 0

Mathlight
Mathlight

Reputation: 6653

try this:

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

Upvotes: 4

Related Questions