2

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.

mikeb
  • 10,578
  • 7
  • 62
  • 120

1 Answers1

2

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

Juls
  • 658
  • 6
  • 15
  • You'll see I have an array, not a String – mikeb Sep 02 '20 at 23:23
  • then use length([array var]). Something i found out working through this is that all you can evaluate is TRUE|FALSE. There is no if (x > 3). X is false if its null or 0, and true if its 1 or more. The best answer I read was that StringTemplte4 is the View within the MVC framework. And (x > 3) amounts to controller logic. – Juls Sep 03 '20 at 15:33