tomj74 / chunk-templates

Chunk Templates, a template engine for Java
http://www.x5software.com/chunk/
MIT License
77 stars 10 forks source link

{% if ... %} testing for empty container #8

Closed slisznia closed 9 years ago

slisznia commented 9 years ago

On occasion, I'd like to test if a container is empty using if-directive instead of inside a loop-directive's onEmpty. Is it possible to do this in Chunk?

tomj74 commented 9 years ago

I believe you can use the length filter for this:

{% if ($x|length == 0) %} ... {% endif %}

Even if {$x} is undefined the filter will yield 0. Can use "length" or the "len" alias. Also just as a warning, the == test is for string equality.