简体   繁体   English

Stringtemplate 4 (st4) - 如果字符串不为空则渲染

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

I need an st4 expression to render an element if a string is not empty:如果字符串不为空,我需要一个 st4 表达式来呈现元素:

<if(theString)>...<endif> does not work - it seems to only work for arrays. <if(theString)>...<endif>不起作用 - 它似乎只适用于数组。

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.我试过if(theString.length > 0)和各种东西,但看起来这只适用于未设置的变量或数组。

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' .我希望能够测试像obj.Name == 'foobar'这样的条件。 Not sure if stringtemplate-4 can do it?不确定 stringtemplate-4 是否可以做到?

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.但是要找到字符串的长度,您可以使用strlen函数。

ie. 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这是所有功能的有用列表: https : //github.com/antlr/stringtemplate4/blob/master/doc/cheatsheet.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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