StringTemplate.Net 学习笔记(2):语法介绍(表格)

表达式元素语法:

Syntax Description(注:中文部分为个人理解)
<attribute> Evaluates to the value of attribute.ToString() if it exists else empty string.
调用attribute.ToString()方法,若attribute不存在,则显示空字符串
<i>, <i0> The iteration number indexed from one and from zero, respectively, when referenced within a template being applied to an attribute or attributes.
集合的当前索引值,<i>从1开始计算,<i0>从0开始计算,如果当前集合为null,则显示空字符串
<attribute.property> Looks for property of attribute as a property (C#), then accessor methods like getProperty() or isProperty(). If that fails, StringTemplate looks for a raw field of the attribute called property. Evaluates to the empty string if no such property is found.
获取attribute的property属性值,如user.Name
<attribute.(expr)> Indirect property lookup. Same as attribute.property except use the value of expr as the property_ name. Evaluates to the empty string if no such property is found.
同上,区别在于字段为保留字的情况下使用,如user.("Name")
<multi-valued-attribute> Concatenation of ToString() invoked on each element. If multi-valued-attribute is missing his evaluates to the empty string.
调用集合中所有非null元素的ToString方法,再把它们连接起来
<multi-valued-attribute; separator=expr> Concatenation of ToString() invoked on each element separated by expr.
同上,区别在于,连接的时候在相邻的两个元素之间使用了分隔符
<[mine, yours]> Creates a new multi-valued attribute (a list) with elements of mine first then all of yours.
把mine和yours合并成新的集合,不限于mine、yours两个元素。对于不同类型的mine和yours,都会转换成数组形式。其他用法同上
<template(argument-list)> Include template. The argument-list is a list of attribute assignments where each assignment is of the form arg-of-template=expr where expr is evaluated in the context of the surrounding template
not of the invoked template.
调用模板,传递参数(若有参数),template为模板名称,如template()
<(expr)(argument-list)> Include template whose name is computed via expr. The argument-list is a list of attribute assignments where each assignment is of the form attribute=expr. Example $(whichFormat)()$ looks up whichFormat's value and uses that as template name. Can also apply an indirect template to an attribute.
同上,区别在于模板名称为关键字的时候,如("template")()
<attribute:template(argument-list)> Apply template to attribute. The optional argument-list is evaluated before application so that you can set attributes referenced within template. The default attribute it is set to the value of attribute. If attribute is multi-valued, then it is set to each element in turn and template is invoked n times where n is the number of values in attribute. Example: $name:bold() applies bold() to name's value.
这是一种循环语法,对集合中每一个元素都应用template模板(若模板有参数则传递参数)
<attribute:(expr)(argument-list)> Apply a template, whose name is computed from expr, to each value of attribute. Example $data:(name)()$ looks up name's value and uses that as template name to apply to data.
同上,区别在于模板名称为关键字的时候
<attribute:t1(argument-list): ... :tN(argument-list)> Apply multiple templates in order from left to right. The result of a template application upon a multi-valued attribute is another multi-valued attribute. The overall expression evaluates to the concatenation of all elements of the final multi-valued attribute resulting from templateN's application.
同<attribute:template(argument-list)>,区别在于,对集合中每一个元素都按从左到右的顺序应用了多个模板
<attribute:{anonymous-template}> Apply an anonymous template to each element of attribute. The iterated it atribute is set automatically.
匿名模板,即内联式的,跟匿名委托有些类似,这是一种循环语法,集合的当前元素名称自动设置为it
<attribute:{argument-name_ | _anonymous-template}> Apply an anonymous template to each element of attribute. Set the argument-name to the iterated value and also set it.
同上,区别在于,手动设置集合的当前元素名称
<a1,a2,...,aN:{argument-list_ | _anonymous-template}> Parallel list iteration. March through the values of the attributes a1..aN, setting the values to the arguments in argument-list in the same order. Apply the anonymous template. There is no defined it value unless inherited from an enclosing scope.
多个参数的匿名模板,必须自定义每个集合的当前元素名称
<attribute:t1(),t2(),...,tN()> Apply an alternating list of templates to the elements of attribute. The template names may include argument lists.
交替调用模板,并应用到attribute如:$list:style1():style2:style3()$,
对第一个元素应用style1,第二个元素应用style2,第三个元素应用style3,依此类推
<first(attr)> The first or only element of attr. You can combine operations to say things like first(rest(names)) to get second element.
取得集合的第一个元素
<last(attr)> The last or only element of attr.
取得集合的最后一个元素
<rest(attr)> All but the first element of attr. Returns nothing if $attr$ a single valued.
取得集合中除第一个元素之外的所有元素,如果attribute不是集合,则显示空字符串
<trunc(attr)> returns all but last element
取得集合中除最后一个元素之外的所有元素,其他同上
<strip(attr)> Returns an iterator that skips any null values in $attr$. strip(x)
=x when x is a single-valued attribute.
取得集合中所有不为null的元素,如果attribute不是集合,则显示attr的值
<length(attr)> Return an integer indicating how many elements in length $attr$ is. Single valued attributes return 1. Strings are not special; i.e., length("foo") is 1 meaning "1 attribute". Nulls are counted in lists so a list of 300 nulls is length 300. If you don't want to count nulls, use length(strip(list)).
取得集合的元素个数,即集合的长度
\$ or \< escaped delimiter prevents $ or < from starting an attribute expression and results in that single character.
对美元号$或尖括号<进行转义,当它们作为表达式分隔符的时候。默认为$...$,可以设置成一对尖括号<...>
<\ >, <\n>, <\t>, <\r> special character(s): space, newline, tab, carriage return. Can have multiple in single <...> expression.
要在表达式中使用空格、换行符、制表符、回车符时,必须对它们进行转义,如输出换行符:<\n>,
也可以在单行表达式<...>里输出多个,如:<\n\r\t>
<\uXXXX> Unicode character(s). Can have multiple in single <...> expression.
Unicode字符,如<\u5555>,也可以在单行表达式<...>输出多个,如:<\u56E7\u6708>
<! comment !>, $! comment !$ Comments, ignored by StringTemplate.
注释,分隔符与默认表达式分隔符一致,支持多行注释

条件声明:

Syntax Description
<if(attribute)>subtemplate
<else>subtemplate2
<endif>
If attribute has a value or is a boolean object that evaluates to true, include subtemplate else include subtemplate2. These conditionals may be nested.
如果属性有值或者属性为bool值true,则表示达成条件,调用subtemplate模板,否则调用subtemplate2模板,这些条件可以嵌套
<if(x)>subtemplate
<elseif(y)>subtemplate2
<elseif(z)>subtemplate3
<else>subtemplate4
<endif>
First attribute that has a value or is a boolean object that evaluates to true, include that subtemplate. These conditionals may be nested.
<if(!attribute)>subtemplate<endif> If attribute has no value or is a bool object that evaluates to false, include subtemplate. These conditionals may be nested.

保留字:

不要使用这些保留字作为属性名称或模板名称(模板组、模板组接口不在此列)

default
first
group
if
implements
interface
last
length
optional
rest
strip
super
trunc
else
endif
elseif

样式真是无语啊,还是不会用这博客!

本文地址:http://www.cnblogs.com/lwme/archive/2010/04/29/1724186.html

英文来自:http://www.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet

这个表格更详细:http://www.antlr.org/wiki/display/ST/ST+condensed+--+Templates+and+expressions#STcondensed--Templatesandexpressions-TemplateExpressionsandStatements

原文地址:https://www.cnblogs.com/lwme/p/1724186.html