在Android的使用大块模板引擎的自定义标签 [英] Using chunk template engine in android with custom tags
问题描述
我想在Android中使用的块。我需要的是这样的:
I am trying to use chunk in android. I need something like this:
假设,
以下是标签。
标签: {世界:世界,C:丹尼斯里奇,苹果:乔布斯}
tags: {"world":"WORLD", "c": "Dennis Ritchie", "apple":"JOBS" }
输入: HELLO {{世界}},C是写的 {{c}里} ,而Java是由{{java的}},HOLA写
Input: HELLO {{ world }}, C is written by {{ c }}, while java is written by {{ java }}, hola.
输出: HELLO WORLD,C是由丹尼斯里奇写的,而Java写的按,HOLA
Output: HELLO WORLD, C is written by Dennis Ritchie, while java is written by, hola.
在短期
-
我需要一个自定义分隔符一样,{{字符串}}
即
I need a custom delimiter like, {{ string }} i.e.
DEFAULT_TAG_START ={{;
DEFAULT_TAG_START = "{{";
DEFAULT_TAG_END =}};
DEFAULT_TAG_END ="}}";
而如果输入中包含未指定标签,那么它应该被替换为空。
While if input contains tag which is not specified, then it should be replaced by empty.
我试过放&;被困在下面,
I tried & stuck at following,
public String process(String msg) {
Chunk c = new Chunk();
c.append(msg);
c.set("apple", "JOBS");
c.set("c", "Dennis Ritchie");
c.set("world", "WORLD");
return c.toString();
}
有人
可以知道该怎么办呢?
Can anybody knows how to do it?
感谢名单中andvance。
Thanx in andvance.
推荐答案
块模板引擎不支持在目前备用标记语法。此外,标签标志中的空格不会被忽略/丢弃。
The Chunk template engine does not support alternate tag syntax at the moment. Also, whitespace within tag markers is not ignored/discarded.
不过,这里有一个可能的桥接解决方案。未提供该块标签将默认只要变量名后跟一个冒号空。
However, there is a possible bridge solution here. Chunk tags that are not provided will default to empty as long as the tag name is followed by a colon.
所以你的输入必须改变,以有效块语法:
So your input must change to valid Chunk syntax:
HELLO {$world:}, C is written by {$c:}, while java is written by {$java:}, hola.
如果您的模板语法是不灵活(例如,你有pre-现有模板库,或者你只是真的不喜欢原生标记语法),你可以添加一个pre-处理步骤进行改造 {{}这个}
到 {$这样的:}
才追加到块模板
If your template syntax is not flexible (eg, you have a library of pre-existing templates, or you just really dislike the native tag syntax) you could add a pre-processing step to transform {{ this }}
into {$this:}
before appending it to the Chunk template.
块库甚至还提供了方便的功能会为你做到这一点(虽然你可能需要重新实现它,如果输入的是空格不一致)。
The Chunk library even provides a convenience function that will do this for you (although you may need to reimplement it if the input is inconsistent with whitespace).
import com.x5.template.TemplateSet;
...
Chunk c = new Chunk();
String template = TemplateSet.convertTags(msg, "{{ ", " }}", "{$", ":}");
c.append(template);
c.set("apple", "JOBS");
c.set("c", "Dennis Ritchie");
c.set("world", "WORLD");
return c.toString();
这篇关于在Android的使用大块模板引擎的自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!