StringTemplate.Net 學習筆記(2):文法介紹(表格)

來源:互聯網
上載者:User

運算式元素文法:

Syntax
Description(註:中文部分為個人理解)

attribute>
Evaluates to the value of attribute.ToString() if it exists else empty string.
調用attribute.ToString()方法,若attribute不存在,則顯示Null 字元串

,
The iteration number indexed from one and from zero, respectively, when referenced within a template being applied to an attribute or attributes.
集合的當前索引值,從1開始計算,從0開始計算,如果當前集合為null,則顯示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,依此類推


The first or only element of attr. You can combine operations to say things like first(rest(names)) to get second element.
取得集合的第一個元素


The last or only element of attr.
取得集合的最後一個元素


All but the first element of attr. Returns nothing if $attr$ a single valued.
取得集合中除第一個元素之外的所有元素,如果attribute不是集合,則顯示Null 字元串

returns all but last element
取得集合中除最後一個元素之外的所有元素,其他同上


Returns an iterator that skips any null values in $attr$. strip(x)
=x when x is a single-valued attribute.
取得集合中所有不為null的元素,如果attribute不是集合,則顯示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.
對美元號$或角括弧

>, , ,
special character(s): space, newline, tab, carriage return. Can have multiple in single <...> expression.
要在運算式中使用空格、分行符號、定位字元、斷行符號符時,必須對它們進行轉義,如輸出分行符號:,
也可以在單行運算式<...>裡輸出多個,如:


Unicode character(s). Can have multiple in single <...> expression.
Unicode字元,如,也可以在單行運算式<...>輸出多個,如:

, $! comment !$
Comments, ignored by StringTemplate.
注釋,分隔字元與預設運算式分隔字元一致,支援多行注釋

條件聲明:

Syntax
Description

attribute)>subtemplate
subtemplate2

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模板,這些條件可以嵌套

x)>subtemplate
y)>subtemplate2
z)>subtemplate3
subtemplate4

First attribute that has a value or is a boolean object that evaluates to true, include that subtemplate. These conditionals may be nested.

attribute)>subtemplate
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.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet

這個表格更詳細:http://www.antlr.org/wiki/display/ST/ST+condensed+--+Templates+and+expressions#STcondensed--Templatesandexpressions-TemplateExpressionsandStatements

轉自:http://www.cnblogs.com/lwme/archive/2010/04/29/1724186.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.