Stringtemplate Study Notes

Source: Internet
Author: User
Stringtemplate is a template engine that also supports Java, C #, Python. You can download from the http://www.stringtemplate.org.
Stringtemplate syntax description

The stringtemplate syntax is divided by $ XXXX $. The key write of stringtemplate is case sensitive.

Attribute reference
Name attribute
In a template, this is the most common attribute used to display text as follows:

Your email address: $ email $

Replace the value of the property name email.

The same attribute can have multiple values. The syntax for multiple values is as follows:
$ Value; null = "XXX", separator = "," $
Defines the value attribute. If the value is null, XXX is displayed. If there are multiple attribute values, separate them with commas (,).

Field Reference
If an attribute name is an object or set, you can use the attribute name. Field name to access the field value.
For example:
Your name: $ person. Name $
Your Email: $ people. Email $

Syntax: $ object name. Field name $
In C #, you can directly set an object to a property name.
For example:
User US = new user ();
Us. Name = "xxsssx ";
Us. value = "80 ";

Stringtemplate ST = new stringtemplate ("$ user. Name $, $ user. value $ ");
St. setattribute ("user", US );

Console. writeline (St. tostring ());
For key/value objects, the preceding access method is also used, for example:
Stringtemplate A = new stringtemplate ("$ user. Name $, $ user. Phone $ ");
Hashtable user = new hashtable ();
User. Add ("name", "Terence ");
User. Add ("phone", "none-of-your-business ");
A. setattribute ("user", user );
String Results = A. tostring ();

Custom attribute field name
Format: $ attribute name: {It. Field name} $
For example:
Stringtemplate ST = new stringtemplate ("$ abcdef: {First: $ it. ddddd $ Second: $ it. ddddd $} $ ");
St. setattribute ("abcdef. {ddddd, ddddd}", "Chinese", "I'm not ");
Console. writeline (St. tostring ());

If the field name is a reserved word, you can use $ object name. ("reserved word") $

Display multiple attributes at a time
$ [Attribute name, attribute name] $

Template reference
You must add templates to the same template group to call templates between them.
Use $ Template Name () $ to call the Template
Template parameters
$ Template Name (parameter = parameter value, parameter name = parameter value) $
For example:
Stringtemplategroup group = new stringtemplategroup ("Temp ");
Group. definetemplate ("Link", "<a href = '$ URL $'> $ title $ </a> ");
Stringtemplate ST = new stringtemplate (group, "Call the link template to display the link $ Link (url = \"/faq/view? Id = \ "+ faqid, Title = faqtitle) $, really! ");
St. setattribute ("faqid", 1 );
St. setattribute ("title", "People's Republic of China ");
Console. writeline (St. tostring ());

Loop display usage
User US = new user ();
Us. Name = "Haha ";
Us. value = "99 ";
List <user> USS = new list <user> ();
USS. Add (US );
USS. Add (US );
USS. Add (US );
USS. Add (US );
Stringtemplate ST = new stringtemplate ("<Table> $ User: {<tr> $ it. name $ <TD> </TD> $ it. value $ </TR >}$ </table> ");
St. setattribute ("user", USS );
Console. writeline (St. tostring ());

Alternate display by template
Stringtemplategroup group = new stringtemplategroup ("test ");
Group. definetemplate ("trred", "<tr class = red> <TD> $ it. name $ </TD> <TD> $ it. value $ </TD> </tr> \ n ");
Group. definetemplate ("trwither", "<tr class = wither> <TD> $ it. name $ </TD> <TD> $ it. value $ </TD> </tr> \ n ");

Stringtemplate ST = new stringtemplate (group, "<Table> $ User: trred (), trwither () $ </table> ");
User US = new user ();
Us. Name = "HAHAHA ";
Us. value = "999 ";
List <user> USS = new list <user> ();
USS. Add (US );
USS. Add (US );
USS. Add (US );
St. setattribute ("user", USS );
Console. writeline (St. tostring ());

Download the example Tutorial: Download

Appendix: English syntax

Syntax

Description

<Attribute>

Evaluates TO THE VALUEAttribute. Tostring () if it exists else empty string.

<I>, <I0>

The iteration number indexed from one and from zero, respectively, when referenced within a template being applied to an attribute or attributes.

<Attribute.Property>

LooksPropertyOfAttributeAs a property (C #), then accessor methods like getProperty() Or isProperty(). If that fails, stringtemplate looks for a raw field ofAttributeCalledProperty. Evaluates to the empty string if no such property is found.

<Attribute.(Expr)>

Indirect property lookup. SameAttribute.PropertyCannot use the valueExprAs the property _ name. evaluates to the empty string if no such property is found.

<Multi-valued-attribute>

Concatenation of tostring () invoked on each element. IfMulti-valued-attributeIs missing his evaluates to the empty string.

<Multi-valued-attribute; Separator =Expr>

Concatenation of tostring () invoked on each element separatedExpr.

<Template(Argument-list)>

IncludeTemplate.Argument-listIs a list of attribute assignments where each assignment is of the formARG-Of-template=ExprWhereExprIs evaluated in the context of the surrounding Template
Not of the invoked template.

<(Expr)(Argument-list)>

IncludeTemplateWhose name is computedExpr.Argument-listIs a list of attribute assignments where each assignment is of the formAttribute=Expr. Example $ (whichformat) () $ looks up whichformat's value and uses that as Template Name. can also apply an indirect template to an attribute.

<Attribute:Template(Argument-list)>

ApplyTemplateToAttribute. The optionalArgument-listIs evaluated before application so that you can set attributes referencedTemplate. The default attribute it is set to the valueAttribute. IfAttributeIs multi-valued, then it is set to each element in turn andTemplateIs invokedNTimes whereNIs the number of values inAttribute. Example: $ name: Bold () applies bold () to name's value.

<Attribute:(Expr)(Argument-list)>

Apply a template, whose name is computed fromExpr, To each valueAttribute. 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 fromTemplaten'S application.

<Attribute:{Anonymous-template}>

Apply an anonymous template to each elementAttribute. The iterated it atrially is set automatically.

<Attribute:{Argument-name _ | _ anonymous-template}>

Apply an anonymous template to each elementAttribute. SetArgument-nameTo the iterated value and also set it.

<A1,A2,...,An:{Argument-LIST _ | _ anonymous-template}>

Parallel list iteration. march through the values of the attributesA1..An, Setting the values to the arguments inArgument-listIn 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 elementsAttribute. The template names may include argument lists.

<If (Attribute)>Subtemplate
<Else>Subtemplate2
<Endif>

IfAttributeHas a value or is a bool object that evaluates to true, includeSubtemplateElse includeSubtemplate2. These conditionals may be nested.

<If (!Attribute)>Subtemplate<Endif>

IfAttributeHas no value or is a bool object that evaluates to false, includeSubtemplate. These conditionals may be nested.

<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.

<Strip (ATTR)>

Returns an iterator that skips any null values in $ ATTR $. Strip
= X when X is a single-valued attribute.

<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 characters: space, newline, tab, carriage return.

<! Comment!>, $! Comment! $

Comments, ignored by stringtemplate

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.