If statement in jQuery template

The following code returns the "Unused ReferenceError: Undefined Size" error in Chrome if the size of the variable is undefined:

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

So far, this code is working fine :

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

Is there any way to make it work in Chrome without using the double if statement, and why does it even return an error?

+4
source share
2 answers

try the following:

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

try it

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

Pay attention to the operator space after if.

0
source

Source: https://habr.com/ru/post/1433546/


All Articles