Use of the vtemplate engine-advanced

Source: Internet
Author: User

1. Differences between <Vt: Template> and <Vt: Include> labels

The <Vt: Template> and <Vt: Include> tags both contain the file attribute. If both tags are set with the file attribute, the two tags look very similar, in addition, the final result is to include the content of the file. However, for the template engine, the difference between them is very large.

<Vt: Template> A tag is a "template block" tag that can have its own "variables". It becomes the "ownertemplate" of its internal labels ). <Vt: Include> simply includes the file content. The "host template" of its internal labels is the same as that of its internal labels.

Assume that there is a VT template file: inc_content.html

I am the variable {$: #. var1} in the file }.
I am the foreach label in the file:
<Vt: foreach from = "$ #. Names" item = "#. Name" index = "#. I">
The {$: #. I} Name of the contained file is {$: #. name }.
</VT: foreach>

The <Vt: Template> and <Vt: Include> labels are used to include the above files, as follows:

A. <Vt: Template> includes:

I am an external variable {$: #. var1 }.
I am an external foreach label:
<Vt: foreach from = "$ #. Names" item = "#. Name" index = "#. I">
The external {$: #. I} Name is {$: #. name }.
</VT: foreach>
<Vt: Template id = "Inc" file?”inc_content.html "/>

B. <Vt: Include> includes:

I am an external variable {$: #. var1 }.
I am an external foreach label:
<Vt: foreach from = "$ #. Names" item = "#. Name" index = "#. I">
The external {$: #. I} Name is {$: #. name }.
</VT: foreach>
<Vt: Include id = "Inc" file=”inc_content.html "/>

The above two VT template codes look very similar, but after resolution, var1 in A and var1 in the <Vt: Template> template block exist independently of each other! The var1 variable in B is equal to the var1 variable in INC <Vt: Include>, and all variables reference the same variable (similar to other variables ).

Assume that the VT template code of Block A and block B is handled by the following program:

This. Document. variables. setvalue ("var1", 1 );
This. Document. variables. setvalue ("names", new string [] {"Zhang San", "Li Si", "Wang Wu "});

That is, only assign values to external variables var1 and names. After the template engine parses the output, the output results are as follows:

The figure shows that <Vt: Template> contains no data output, while <Vt: Include> contains data output, which is exactly the same as external data! Therefore, you can regard the <Vt: Template> label as a class in the program language. It can own its own variables. Changing the value of an external variable does not affect its internal variables with the same name, and the external tag can get its internal variables through its ID!

For specific sample code, see: http://net-vtemplate.googlecode.com/svn/src/VTemplate.WebTester/template_include_test.ashx.cs

 

 

2. Use a variable expression

Variable expressions can be used in tag attributes or in variable elements. It is used to obtain the result value of a field, attribute, function method, or index in a variable. For example, #. var1 in the preceding example indicates that the value of the variable var1 is obtained, that is, the value "1 ".

For fields, attributes, or function methods that actually exist in the variable value type, the VT template engine obtains the result value through reflection. For example, the following VT template code:

My name is {$: User. name}. I am from {$: User. Age}, {$: User. Location. getcity ()}

If the user variable is assigned the following class instance values, the template engine can correctly parse the final values of each variable expression when the template code above is parsed.

Class location
{
Public String getcity (){
// Code here
}
}

Class user
{
Public string name {Get; set ;}
Public int age {Get; set ;}
Public location {Get; set ;}
}

However, in some cases, the "value" we need to obtain does not simply exist in the type of the variable value, but the value that needs to be processed by other operations. For example, in the above personal data acquisition, we also need to obtain the user's total personal property, but from the code above, we can see that the total personal property does not exist in the user class, therefore, the VT template engine cannot obtain the value of this item. How can we obtain this data? The VT template engine provides a method to manually set the value of a variable expression. What we need to do is to manually set the value of a variable expression based on this method! For example, the above VT template code is changed to the following:

My name is {$: user. name}, This year {$: user. age} years old, I am from {$: user. location. getcity ()}, my personal property total has {$: user. totalmoney} RMB.

From the above example code, we can know that the totalmoney item does not exist in the user's attribute/field list, so we need to manually set the {$: User. totalmoney} value. The sample code is as follows:

/// <Summary>
/// Return the total personal property of a user
/// </Summary>
/// <Param name = "user"> </param>
/// <Returns> </returns>
Static int getusertotalmoney (User user)
{
// Code here
}

// ------------------------ Use the code ----------------------------------------//

// Get the user variable
Variable uservar = This. Document. Variables ["user"];
// Generate a user instance
User user = new user ();
//............ Skip other codes ............ //
// Set the value of the user variable to the user instance
Uservar. value = user;
// Manually set the value of totalmoney (note that the order between this line and the above line cannot be messy)
Uservar. setexpvalue ("totalmoney", getusertotalmoney (User ));

 

 

3. Conditional Control Data Output

When outputting data, we do not simply output all the data, but need to obtain part of the data according to a combination of many external conditions. For these external conditions, if they can be fixed, we can write them into the attributes of the tag (recommended <Vt: Template> tag) when designing the VT template, in this way, we can obtain these external conditions in the program code and process the data.

For example, the data in the "related news" and "hot news" columns on the right of the news channel in the blog Park, such:

HypothesisThe news obtained in "related news" belongs to the "relating" type, while "hot news" is the news of the "hoting" type, then we can design the VT template as follows:

<Div class = "side_block">
<H3 class = "title_blue"> related news <Vt: Template Name = "topnews" type = "relating" file = "cnblogs_newsdata.html"/>
</Div>
<Div class = "side_block">
<H3 class = "title_yellow"> hot news <Vt: Template Name = "topnews" type = "hoting" file = "cnblogs_newsdata.html"/>
</Div>

In the above VT template, two <Vt: Template> labels with the name "topnews" are defined to facilitate the two <Vt: template> name defined for unified processing (because the data to be processed is the same, but the conditions for obtaining data are different. The custom property type is defined for data acquisition. The VT template containing the cnblogs_newsdata.html file is as follows:

<Ul class = "topnews block_list bt">
<Vt: foreach from = "$ #. newsdata" item = "#. News" Index = "#. I" id = "newslist">
<Li>
<A href = "{$ :#. news. URL} "Title =" {$ :#. news. title htmlencode = 'true'} ">{$ :#. news. title htmlencode = 'true '}... </a>
</LI>
</VT: foreach>
</Ul>

In the VT template of this file, a <Vt: foreach> label with ID "newslist" is defined, this ID is defined to control the output of news and the access address for processing each news in the program code, that is, "{$ :#. news. URL} "variable expression value.

Sample Code:

// Obtain all template blocks named topnews
Elementcollection <template> templates = This. Document. getchildtemplatesbyname ("topnews ");
Foreach (template in templates)
{
// Obtain news data based on the Type attribute conditions defined in the template Block
List <News> newsdata = getnewsdata (template. Attributes. getvalue ("type "));
// Set the value of the variable newsdata
Template. variables. setvalue ("newsdata", newsdata );

// Obtain the newslist' tag under the template (that is, the foreach tag defined in the cnblogs_newsdata.html file)
Tag tag = template. getchildtagbyid ("newslist ");
If (tag is foreachtag)
{
// If the label is foreach, set its beforerender event to set the value of variable expression {$: #. News. url }.
Tag. beforerender + = (sender, e) =>
{
Foreachtag t = (foreachtag) sender;
// Obtain the value of the current item (because the data source of the foreach tag is a list <News> set, the value type of the current item is news entity)
News news = (News) T. item. value;
// Set the value of the variable expression of the current item. That is, the variable expression "{$: #. News. url }".
T. item. setexpvalue ("url", getnewsurl (News ));
};
}
}

The beforerender event is used in the code above, which is triggered before the data of the tag element is rendered. For Loop elements <Vt: foreach> and <Vt: For>, this event (including the afterrender event) is triggered each time the data is presented during each loop ), therefore, we can use this event method to obtain the value of the current loop item.

For specific sample code, see: http://net-vtemplate.googlecode.com/svn/src/VTemplate.WebTester/cnblogs_newslist.ashx.cs

 

The vtemplate project is hosted on Google Code.
URL: http://net-vtemplate.googlecode.com/
SVN: http://net-vtemplate.googlecode.com/svn/src/VTemplate.Engine/

MoreFor example, refer to the vtemplate. webtester Project:

Http://net-vtemplate.googlecode.com/svn/src/VTemplate.WebTester/

Or watch the Online Demo example: (thanks to the netizen"Dolt","Madman"Provide)

Http: // 61.155.39.222: 8888/index. ashx

Note: A vtemplate engine technology exchange QQ group has been established. You are welcome to join the project development or technical discussion. QQ group:884468

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.