【问题标题】:Stringtemplate 4 (st4) - Render if string is not emptyStringtemplate 4 (st4) - 如果字符串不为空则渲染
【发布时间】:2020-03-04 13:23:32
【问题描述】:

如果字符串不为空,我需要一个 st4 表达式来呈现元素:

<if(theString)>...<endif> 不起作用 - 它似乎只适用于数组。

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

我正在遍历一个列表并渲染它,所以我可以在中间有值是我不想渲染任何东西的空字符串。

【问题讨论】:

    标签: stringtemplate stringtemplate-4


    【解决方案1】:

    我一直在努力寻找在条件中比较字符串的能力。我希望能够测试像obj.Name == 'foobar' 这样的条件。不知道stringtemplate-4能不能做到?

    如果值不为空,则要呈现,您应该能够执行以下操作。

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

    但要查找字符串的长度,您可以使用strlen 函数。

    即。

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

    这里是所有functions 的有用列表: https://github.com/antlr/stringtemplate4/blob/master/doc/cheatsheet.md

    【讨论】:

    • 你会看到我有一个数组,而不是一个字符串
    • 然后使用长度([array var])。我发现解决这个问题的是,您可以评估的所有内容都是 TRUE|FALSE。没有 if (x > 3)。 X 如果为 null 或 0,则为 false,如果为 1 或更多,则为 true。我读到的最佳答案是 StringTemplte4 是 MVC 框架中的视图。并且 (x > 3) 相当于控制器逻辑。
    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多