Stringtemplate 4 (st4) - Render if string is not empty

1.1k Views Asked by At

I need an st4 expression to render an element if a string is not empty:

<if(theString)>...<endif> does not work - it seems to only work for arrays.

I've tried if(theString.length > 0) and all kinds of stuff, but it looks like it this only works for variables that are not set or for arrays.

I am iterating over a list and rendering this, so I could have values in the middle that are empty strings that I don't want to render anything at all for.

1

There are 1 best solutions below

2
Juls On

I've been struggling trying to find the ability to compare strings in a conditional. I want to be able to test for a condition like obj.Name == 'foobar'. Not sure if stringtemplate-4 can do it?

To render if the value is not null you should be able to do the following.

$if(obj.Name)$ $obj.Name$ $endif$    // prints the Name property of obj

But to find the length of a string you can use strlen function.

ie.

// obj.Name = "foobar", prints 'Length of Name is 6'
$if(strlen(obj.Name))$ Length of Name is $strlen(obj.Name)$ $endif$  

Here is a helpful list of all the functions: https://github.com/antlr/stringtemplate4/blob/master/doc/cheatsheet.md