问题
I am trying to use chunk in android. I need something like this:
Suppose,
Following are tags.
tags: {"world":"WORLD", "c": "Dennis Ritchie", "apple":"JOBS" }
Input: HELLO {{ world }}, C is written by {{ c }}, while java is written by {{ java }}, hola.
Output: HELLO WORLD, C is written by Dennis Ritchie, while java is written by, hola.
In short
I need a custom delimiter like, {{ string }} i.e.
DEFAULT_TAG_START = "{{";
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();
}
回答1:
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.
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();
来源:https://stackoverflow.com/questions/24238187/using-chunk-template-engine-in-android-with-custom-tags