Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 201
bogatyrjov Avatar asked Oct 07 '22 16:10

bogatyrjov


1 Answers

try this:

<script type="text/x-jquery-tmpl">    
    {{if name && size != null && size}}
        <p>${name}</p>
        <p>${size}</p>
    {{/if}}
</script>
like image 167
Mathlight Avatar answered Oct 10 '22 12:10

Mathlight