CommonTemplate(http://www.commontemplate.org)已提供了注释版和属性版语法外套.
注释版语法外套:
<html> <body> <!--$if{users != null && users.size &gt; 0}--> <table border="1"> <!--$for{user : users}--> <tr> <td><!--$output{for.index + 1}-->1<!--$end--></td> <td><!--$output{user.name}-->james<!--$end--></td> <td><!--$output{user.coins}-->2.00<!--$end--></td> </tr> <!--$end--> </table> <!--$end--> </body> </html>
属性版语法外套:
<html> <body> <table ct:if="users != null && users.size &gt; 0" border="1"> <tr ct:for="user : users"> <td><span ct:output="for.index + 1">1</span></td> <td><span ct:output="user.name">james</span></td> <td><span ct:output="user.coins">2.00</span></td> </tr> </table> </body> </html>
当模板应用于XML/HTML时, 可能需要保持完整的DOM模型结构,
属性版语法外套也可以做到, 但转换比较耗时, 且存在不规则HTML语法兼容问题.
桂林提议加入XML/HTML标签版语法外套, 使用如:
<html> <body> <ct:if param="users != null && users.size &gt; 0"> <table border="1"> <ct:for param="user : users"> <tr> <td><ct:out param="for.index + 1"/></td> <td><ct:out param="user.name"/></td> <td><ct:out param="user.coins"/></td> </tr> </ct:for> </table> </ct:if> </body> </html>
其结构与标准语法是一对一的, 转换起来非常方便.
使用ResourceFilter扩展点进行实现,
在资源加载时使用简单的正则表达式替换,
不解析HTML语法,也就没有不规则HTML语法兼容问题,
只用了两条正则表达式, 就完成了转换:
text = text.replaceAll("\\<ct\\s*\\:\\s*([0-9|_|A-Z|a-z]+)\\s+param\\s*\\=\\s*\\"([^\\"]+)\\"\\s*\\/?\\>", "\\$$1{$2}"); text = text.replaceAll("\\<\\/\\s*ct\\s*\\:\\s*([0-9|_|A-Z|a-z]+)\\s*\\>", "\\$end{$1}");
加载模板资源时有内存消耗和转换时间消耗,但消耗不是很大,
从0.8.7版本开始支持.
参见:
http://www.commontemplate.org
1 楼
jianfeng008cn
2008-09-10
很好,很强大!
2 楼
betafox
2008-10-07
建议加入xml的xpath支持!!
3 楼
xgene
2008-10-09
<!--$output{user.email}-->james@xxx.com<!--$end-->
转换成属性是
<span ct:output="user.email">james@xxx.com</span>
那
<!--$message{"user.id.title"}-->User ID<!--$end-->
转换成属性是什么?
我写成<span ct:message="user.id.title">User ID</span>
没效果
转换成属性是
<span ct:output="user.email">james@xxx.com</span>
那
<!--$message{"user.id.title"}-->User ID<!--$end-->
转换成属性是什么?
我写成<span ct:message="user.id.title">User ID</span>
没效果
4 楼
javatar
2008-10-11
需要加单引号, 否则user.id.title表示变量.
<span ct:message="'user.id.title'">User ID</span>
<span ct:message="'user.id.title'">User ID</span>
5 楼
xgene
2008-10-12
我又来了! 上个问题知道了,现在又有一个问题! 也是例子程序!
如何在这里http://localhost:8080/tpl/view.action?user.id=15
我要是不停的刷新浏览器! 有时会出现数据丢失! 为什么?
如何在这里http://localhost:8080/tpl/view.action?user.id=15
我要是不停的刷新浏览器! 有时会出现数据丢失! 为什么?
6 楼
xgene
2008-10-12
有时刷新几下就出现变量的内容消失了,有时刷新很久才出现
7 楼
javatar
2008-10-13
使用tomcat5.5.25, 6.0.14, 6.0.16, 6.0.18 测试均未重现该问题, 希望能给出详细的重现步骤, 谢谢.
8 楼
xgene
2008-10-13
ct_struts2-0.8.7-sp1.war 更名=>tpl.war,放入apache-tomcat-6.0.16的wabapps中,启动tomcat,到http://localhost:8080/tpl/edit.action?user.id=15页面, 不停的点击IE刷新按钮(快速的连续点击),有时候你会看到输入框内的数据是空的