简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM