Using chunk template engine in android with custom tags

核能气质少年 提交于 2020-01-04 18:13:08

问题


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

  1. I need a custom delimiter like, {{ string }} i.e.

    DEFAULT_TAG_START = "{{";

    DEFAULT_TAG_END ="}}";

  2. 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

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