过滤掉ST4中的空字符串 [英] Filter out empty strings in ST4
本文介绍了过滤掉ST4中的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下示例:
<["foo", "", "bar"]; separator=",">
结果如下:
foo,,bar
但是我需要:
foo,bar
在ST4中使用分隔符进行格式化之前,是否有任何方法可以过滤出空字符串值?
Is there any way to filter out empty string values before formating with separator in ST4?
(在真实代码中,值来自另一个模板,该模板具有<if>
条件,并为模型中不需要的数据返回空结果,并且我不希望将条件移出该模板以保持模板被封装/隔离)
(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.)
推荐答案
我发现有两个辅助功能的解决方法.但这真令人毛骨悚然...
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", "", ""], ",")>
>>
这给出了:
foo,bar,goo
这篇关于过滤掉ST4中的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文