Stringtemplate. Net learning notes (4): expression element syntax (bottom)

Source: Internet
Author: User

6. Anonymous template/Traversal

The syntax of an anonymous template is $ attribute: {anonymous-template} $. You can traverse the attribute and apply this anonymous template to each element to automatically set it to the current element name:

String [] strarray = new string [] {"A", "B", "C"}; stringtemplate ST = new stringtemplate ("$ array: {current element $ it $}; separator =/",/" $ "); ST. setattribute ("array", strarray); console. writeline (St. tostring ());
Output result: Current element a, current Element B, and current element C

If you do not want to use it as the name of the current element, you can set it manually:

String [] strarray = new string [] {"A", "B", "C"}; stringtemplate ST = new stringtemplate ("$ array: {current | current element $ current $}; separator =/",/" $ "); ST. setattribute ("array", strarray); console. writeline (St. tostring ());

All of the above apply a single anonymous template to attribute. The usage of Multiple anonymous templates is the same as that of a single one, except that the number of templates is different.

Apply multiple templates in sequence (from left to right)Apply multiple templates to each attribute element.

Syntax: $ attribute: {anonymous-template1 }:{ anonymous-template2}...: {anonymous-templaten} $, example:

String [] strarray = new string [] {"A", "B", "C"}; stringtemplate ST = new stringtemplate ("$ array: {current element $ it $ }:{ current | '$ current $' }:{ [$ it $]} $ "); ST. setattribute ("array", strarray); console. writeline (St. tostring ());
Output result: ['current element a'] ['current element B '] ['current element C']

Apply multiple templates alternately. Different from sequential applications, each attribute element applies only one template.

Syntax: $ attribute: {anonymous-template1}, {anonymous-template2}..., {anonymous-templaten} $, example:

String [] strarray = new string [] {"A", "B", "C"}; stringtemplate ST = new stringtemplate ("$ array: {current | current element $ current $}, {style 2 $ it $}, {Style 3 $ it $}; separator =/",/" $ "); ST. setattribute ("array", strarray); console. writeline (St. tostring ());
Output result: the current element a, style 2B, and style 3C

You can also apply the alternate template and sequence template at the same time, every other colon ":" is a group:

String [] strarray = new string [] {"A", "B", "C"}; stringtemplate ST = new stringtemplate ("$ array: {Example 1: $ it $}, {Example 2: $ it $ }:{ '$ it $'}, {|$ it $ | }: {[$ it $] },{},{ ($ it $) }$ "); ST. setattribute ("array", strarray); console. writeline (St. tostring ());
Output: ['Type 1 a'] ('Type 1 C ')

All of the above are parameters for traversing a single set. You can useMultiple Parameters:

string[] array1 = new string[] { "a","b","c" };string[] array2 = new string[] { "d","e" };StringTemplate st = new StringTemplate("$array1, array2:{a1,a2|$a1$,$a2$};separator=/"-/"$");st.SetAttribute("array1", array1);st.SetAttribute("array2", array2);Console.WriteLine(st.ToString());
Output result: a, d-B, E-C,

When multiple set parameters are used, if the number of elements in the set is inconsistent, the set with the maximum number of elements prevails.

In addition, you must note that you cannot apply multiple templates to multiple parameters (or you may not have found a method ).

In other words, in addition to it, the currently automatically set element name also sets ATTR, which is translated from the metrics of riccc.

7. Template File

To facilitate the demonstration, put all the template files used in the bin/DEBUG directory (currently running in debug mode)

Create a new template file named template1.st with the following content:

$title$

Call:

Stringtemplate query = new stringtemplate ("$ template1 () $"); query. setattribute ("title", "stringtemplate learning"); console. writeline (query. tostring ());
Output: stringtemplate Learning

You can also directly pass the value:

Stringtemplate query = new stringtemplate ("$ template1 (Title =/" stringtemplate learning/") $"); console. writeline (query. tostring ());

Or pass it through another defined variable:

Stringtemplate query = new stringtemplate ("$ template1 (Title = mtitle) $"); query. setattribute ("mtitle", "stringtemplate learning"); console. writeline (query. tostring ());

If the name of the called template is a reserved word, for example, first, the calling code is $ ("first") () $

Create a new template named anonymous1.st. content:

$it$

Use an anonymous template as a parameter:

Stringtemplate ST = new stringtemplate ("$ anonymous1 (it ={$ name1 $ name2 $}) $"); ST. setattribute ("name1", "Hangzhou"); ST. setattribute ("name2", "month"); console. writeline (St. tostring ());
Output: monthly

Create a new template format1.st with the following content:

'Monthly month'

Create a new template format2.st with the following content:

[$item$]

Pass the template as a parameter:

StringTemplate st = new StringTemplate("$format2(item=format1())$");Console.WriteLine(st.ToString());
Output: ['monthly month']

Apply the template to attribute

Create a new template named user1.st. content:

Name: $ it. Name $, age: $ it. Age $

Declare a User Type

class User {public string Name {get;set;}public int Age {get;set;}}

Single Object:

User u = new user {name = "", age = 1}; stringtemplate ST = new stringtemplate ("$ users: user1 () $"); ST. setattribute ("users", U); console. writeline (St. tostring ());
Output: Name: month, age: 1

Set:

User [] U = new user [] {New User {name = "", age = 1}, new user {name = "", age = 2 }, new User {name = "", age = 3 }}; stringtemplate ST = new stringtemplate ("$ users: user1 (); separator =/" |/"$ "); st. setattribute ("users", U); console. writeline (St. tostring ());
Output: Name: month, age: 1 | Name: Xiaoqiang, age: 2 | Name: Xiaohong, age: 3

Create a template file user2.st with the following content:

$ Title $ name: $ it. Name $, age: $ it. Age $

Passing parameters:

User u = new user {name = "", age = 1}; stringtemplate ST = new stringtemplate ("$ users: user2 (Title = mtitle) $"); ST. setattribute ("users", U); ST. setattribute ("mtitle", "stringtemplate learning"); console. writeline (St. tostring ());
Output: stringtemplate learning name: month, age: 1

I will not demonstrate them one by one. Other usage methods are the same as those of anonymous templates. The difference is:

  1. Template content external
  2. Replace the {anonymous-template} symbol in the template call section with the template file name: Template ()

8. Others

String [] strarray = new string [] {"A", "B", "C", "D", null, "E "}; stringtemplate ST = new stringtemplate ("First: $ first (array) $ last: $ last (array) $ line feed: $ // N $ exclude the first $ rest (array) $ tab $ // T $ exclude the last one $ trunc (array) $ press Enter: $ // r$ ignore empty elements: $ strip (array) $ output the current default expression separator // $ length: $ length (array) $ Unicode Character: $ // u56e7 $ length after the empty element is ignored: $ length (strip (array) $! Comment comment! $ "); ST. setattribute (" array ", strarray); console. writeline (St. tostring ());
Output:
First: a last: E wrap: exclude first bcde tab exclude last ABCD press Enter: Ignore empty element: ABCDE output current default expression separator $ length: 6 Unicode characters: duration: The length after the empty element is ignored: 5

Finally, let's take a simple and complete exampleExpression ElementSummary of the syntax section ~

Create the user0.st file. content:

$! Show user list! $/T $/$ user list -- $/u56e7/u6708 $/$: $ users: user1 (), user2 (), user3 () $/N $ total number of records: $ length (users) $

Create File user1.st, content

$/N $ [No.: $ I $, name: $ it. Name $, age: $ it. Age $]

Create File user2.st, content

$/N $ {No.: $ I $, name: $ it. Name $, age: $ it. Age $}

Create File user3.st, content

$/N $ 'No.: $ I $, name: $ it. Name $, age: $ it. Age $'

Code:

Class user {public string name {Get; set;} public int age {Get; Set ;}} public static void main (string [] ARGs) {user [] U = new user [] {New User {name = "", age = 1}, new user {name = "", age = 2 }, null, new user {name = "", age = 3}, new user {name = "", age = 3}, new user {name = "", age = 3}, new user {name = "light blue", age = 3}, new user {name = "light green", age = 3 }}; stringtemplategroup STG = new stringtemplategroup ("stgroup", environment. currentdirectory); stringtemplate ST = Stg. getinstanceof ("user0"); ST. setattribute ("users", U); console. writeline (St. tostring (); console. readkey (true );}

Output:

$ User list -- monthly $: [No.: 1, name: monthly, age: 1] {No.: 2, name: Xiaoqiang, age: 2} 'No.: 3, name: Xiaohong, age: 3' [No.: 4, name: xiao, age: 3] {No.: 5, name: Xiaobai, age: 3} 'No.: 6, name: light blue, age: 3' [No.: 7, name: light green, age: 3] Total number of records: 8

I'm dizzy. I hope there are no omissions ~

Sleepy, ready to go to bed

Reference: http://www.antlr.org/wiki/display/ST/Expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.