java自定义模板引擎,在Android的使用大块模板引擎的自定义标签

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();

}

Can anybody knows how to do it?

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.

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值