Surge. js updated version 0.2.3 Alpha, added support for the with Tag, removed context reference from the template, and fixed the bug of the if tag.
Remove context
Surge. js compiles the Template into a js function, which receives a context parameter. Therefore, the variables in the template must be attributes in the context, as shown in the following code:
{% for item in context.products %}
The new version does not need to directly reference context in the template, but must be written as follows:
{% for item in products %}
Add with Tag
The with Mark is added in the new version. Unlike the with statement in js, The with Mark in surge. js is similar to returning a name to the variable, for example:
{% with total = products.length %}{{ total }}{% endwith %}
The code is consistent with the following code:
{{ products.length }}
The with Tag supports defining multiple variables, which are separated by spaces or commas (,), as follows:
{% with total = products.length ps = products %}{% endwith %}
Or
{% with total = products.length, ps = products %}{% endwith %}
If tag
The if tag supports multiple forms, such:
The following statement indicates whether products is null, for example, null, [], false.
{% if products %}{% endif %}
The following code is similar to the js judgment statement, with the same effect as above:
{% if products.length > 0 %}{% endif %}
The following statement will convert to js Code:
{% if 0 < product.length < 10 %}{% endif %}{% if 0 < product.length and products.length > 0 %}{% endif %}
Convert to js Code as follows:
if(0 < products.length < 10){}if(0 < products.length && products.length > 0){}
The more complex if tag can contain filter, as shown below:
{% if products.length > 0 and products|somefilter:argument > 0 %}{% endif %}
Convert to js Code:
if(products.length > 0 && somefilter(products, argument) > 0){}