Adertemplate template engine Usage Analysis

Source: Internet
Author: User
Adertemplate can only run on. net2.0.
Here is a simple example: Thank you for your order # order. billfirstname # order. billlastname #.
< BR >
Your order total is: # format (order. Total, "C ")#
< BR >
< AD: If Test = "# Order. shipcountry isnot" Us "#" >
Your order will arrive in 2-3 weeks
< AD: else >
Your order will arrive in 5-7 days
</ AD: If >

The template file can contain expressions, if/elseif/else statements, foreach/For/set statements, and other template files.

Templates API:There are two main classes in the template engine:
Template and templatemanager.
Template contains a simple template file instance, which is used to process the template.

to create a template object template instance, use the static method of template or templatemanager: template = template. fromstring ( string name, string data)
template = template. fromfile ( string name, string filename)

You can then use it to instantiate templatemanager:Templatemanager mngr= NewTemplatemanager (Template );

Or you can do this:Templatemanager mngr=Templatemanager. fromfile (filename );
Templatemanager mngr=Templatemanager. fromstring (Template );

The fromstring method is used. The string template parameter can contain a template.CodeThis method can create a templatemanager from the dynamically generated text without a template file.

You can use setvalue (string name, object value); to process tags in the template file.
Ex:Mngr. setvalue ("Customer",NewCustomer ("Tom","Jackson"));

In this way, you can use the customer tag in the template. You can use any type of object to replace the tag. When the value of the variable is output, the tostring () method will be called.

-ExpressionsExpression (tag, which should be understood in this way)
In the template file, the label is a # Wrapped string:
# Firstname #

This example will output the value of first name. If you want to output #, you only need to write two # lines.
Ex:
Your SS # Is # ssnumber #

Using tags, You can output any variable (object ):
# Somevar #
An attribute or field of the output object:
# Somestring. Length #
Attribute names are case insensitive, so you can: # string. Length # Or # string. Length #
Or call a function: # trim (somename )#
# Customer. firstname. Length #
You can call methods of any object:
# Firstname. substring (0, 5) # Or # customer. isvalid ()#

Ver2.1 allows you to call the specified array entry:
# Somearray [3] #-get the third item of Array
# Hastable ["somekey"] #-obtain the value of somekey from hashtable.

There are some ready-made functions and functions that can be easily added. These functions are:
Equals (obj1, obj2) -Check whether obj1 and obj2 are equal. If Boolean is true or false
Notequals (obj1, obj2) -Return! Equals (obj1, obj2). equivalent to calling: Not (equals (obj1, obj2 ))
Iseven (Num) -Tests whether number is an even number
Isodd (Num) -Tests whether number is an odd number
Isempty (string) -Test whether the string is null. It is the same as equals (string. length, 0 ).
Isnotempty (string) -Test whether the string contains at least 1 Character
Isnumber (Num) -Test whether num is in numeric format.
Toupper (string) -No more
Tolower (string) -No more
Isdefined (varname) -Test whether varname is defined.
Ifdefined (varname, value) -If varname is defined, the value of value is returned. For example: # ifdefined ("name", name) #-If name is defined, the value of name is returned; otherwise, the value of nothing is returned.
Len (string) -Returns the length of the string.
Tolist (collection, property, delim) -Use delim as the separator to convert the collection into a string. prototype is a collection property. If prototype is not empty, the value of this property separated by delim is returned. if no prototype exists, the collection will be used.
Ex:
Suppose the list is as follows: Arraylist list =   New Arraylist ();
List. Add ( " One " );
List. Add ( " Two " );
List. Add ( " Three " );
Template. setvalue ( " Mylist " , List );

You can write in the template as follows: # tolist (mylist ,"&")#
The final output will be: One & Two & Three

Suppose the list is as follows: List. Add ( New Customer ( " Tom " , " Whatever " ));
List. Add ( New Customer ( " Henry " , " III " ));
List. Add ( New Customer ( " Tom " , " Jackson " ));
Template. setvalue ( " Mylist " , List );

In the template: # tolist (mylist, "firstname ",",")#
The output is Tom, Henry, and Tom.

Isnull (OBJ)-Whether the test object is null
Not (boolvalue)--_-No more
IIF (booleanexpression, iftruevalue, iffalsevalue)-Like the ternary expression in C #: booleanexpression? Iftruevalue: iffalsevalue
Ex:
# IIF (isodd (I), "bgcolor = yellow", "bgcolor = red ")#
Will output bgcolor = yellow if I is odd number and bgcolor = red if I is not odd number

Format (object, formatstring)-The tostring (formatstring) of the object will be called ).
Ex:
(If total is currency 1208.45)
# Format (total, "C ")#
Output: $1,208.45
Trim (string)-Omitted
Filter (collection, booleanproperty)-Will return new list from collection for those objects whose booleanproperty property evaluates to true

GT (obj1, obj2) -If obj1> obj2 returns true. obj1 and obj2 must inherit the same type.
LT (obj1, obj2) -If obj1 <obj2 returns true. obj1 and obj2 must inherit the same type.
Compare (obj1, obj2) -If obj1 <obj2,-1 is returned. If obj1 = obj2, 0 is returned. If obj1> obj2, 1. obj1 and obj2 must inherit the same type.
Or (bool1, bool2) -If bool1 is true or bool2 is true, true is returned.
Ex:
# Or (equals (state, "Il"), equals (state, "NY") #-returns true if State = "Il" or state = "NY"
And (bool1, bool2) -No worries
Comparenocase (string1, string2) -Will do case insenstive comparison of string1 and string2 and return true if they are equal
Stripnewlines (string) -Line feed after string
Typeof (object) -Ex: typeof ("hello") Return "string". typeof (3) returns int
CINT (value) -Call convert. toint32 from. Net Library
Cdouble (value) -Convert to double type
Cdate (value) -Convert to the datetime type. Ex: # cdate ("2005-5-1 ")#
Createtypereference (type) -Ex: # Createtypereference ("system. Math"). Round (3.39789 )#
# Createtypereference ("system. Math"). Pi #
Or
< AD: Set Name = "Mymath" Value = "# Createtypereference (" System. Math ")#" />
# Mymath. Round (3.3 )#
# Mymath. Pi #

(Not finished, continued)

Adertemplate Website: http://www.adersoftware.com
Adertemplate dll: http://www.aspxon.com/ShowSoft/15.aspx
Adertemplate source code (including demo ):Adertemplate (DLL) template engine (C # source code)

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.