Filter out empty strings in ST4

蓝咒 提交于 2019-12-02 12:19:38

问题


Consider the following example:

<["foo", "", "bar"]; separator=",">

This gives the result:

foo,,bar

But I need:

foo,bar

Is there any way to filter out empty string values before formating with separator in ST4?

(In real code the values come from another template, which has <if> condition and returns empty result for undesired data from model, and I don't whant to move the condition out of that template to keep templates incapsulated/isolated.)


回答1:


I've found the workaround with two auxilary things. But it is so creepy...

DropEmpty ::= ["": [], default: key]

Separated(l, s, w=false) ::= "<if (DropEmpty.(first(l)))><if (w)><s><endif><first(l)><Separated(rest(l), s, true)><else><if (rest(l))><Separated(rest(l), s, w)><endif><endif>"

MyTemplate() ::= <<
<Separated(["", "foo", "", "bar", "", "", "goo", "", ""], ",")>
>>

This gives:

foo,bar,goo


来源:https://stackoverflow.com/questions/45678168/filter-out-empty-strings-in-st4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!