Use of the vtemplate engine-advanced

Source: Internet
Author: User

On websites, data in a column is often used on multiple pages at the same time. For example, the column list of a news website or an e-commerce website shows navigation on many pages. In ASP. NET web form development, "Data zones" that are used by multiple pages at the same time often encapsulate these "Data zones" as independent Web controls. What should we do in the vtemplate engine? The vtemplate engine provides a "template block parser" interface. When the vtemplate engine parses and presents the VT template data, if a <Vt: Template> tag has defined the "template block parser ", the "template block parser" instance will be built and the resolution right will be handed over to the "template block parser", and the tag data will continue to be parsed after processing. The flowchart is as follows:

 

In the <Vt: Template> label, the "template block parser" uses the two defined special attributes:

Name Description
Render Defines the instance used to process data in this module. The format is "class instance, assembly ". If you have defined this attribute but have not defined the rendermethod attribute, the class instance must have implemented the itemplaterender interface. (Not defined)
Rendermethod Defines the method of the class instance used to process the data of this module. This method must have the templaterendermethodattribute attribute marked. (Not defined)

If you use the itemplaterender interface to implement the "template block parser", you only need to define the render attribute. If you use the templaterendermethodattribute feature to implement the "template block parser ", the render and rendermethod methods must be defined!

Note:Templaterendermethodattri has the highest priority. If both attributes are defined, the itemplaterender interface is ignored no matter whether the class defined by render has been implemented or not. Instead, the method is directly used!

1. itemplaterender interface.

/// <Summary>
/// Interface for parsing and processing template block data
/// </Summary>
Public interface itemplaterender
{
/// <Summary>
/// Pre-process parsing template data
/// </Summary>
/// <Param name = "template"> </param>
Void prerender (template );
}

As shown in the preceding figure, the itemplaterender interface has only one declaration method, prerender, which is used to receive the template block to be parsed after being transferred by the processing vtemplate engine.

Let's change the last example of "use of vtemplate engine-advanced" and use the itemplaterender interface to process the news list data.
First, we construct a class that has implemented the itemplaterender interface. The Code is as follows:

Namespace vtemplate. webtester. core {// <summary> /// template block parser /// </Summary> public class cnblogsnewsrender: itemplaterender {# region itemplaterender member /// <summary> /// parse the data of a template block /// </Summary> /// <Param name = "template"> </param> Public void prerender (template) {// obtain the news data list <News> newsdata = newsdbprovider according to the type attribute conditions defined in the template block. getnewsdata (template. attributes. getvalue ("type"); // sets the variable new Sdata value template. variables. setvalue ("newsdata", newsdata); // you can obtain a tag (that is, the foreach tag defined in the cnblogs_newsdata.html file. getchildtagbyid ("newslist"); If (tag is foreachtag) {// If the tag is a foreach tag, set its beforerender event to set the variable expression {$ :#. news. URL} value 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, so the value type of the current item is news entity) News news = (n EWS) T. item. value; // set the value of the variable expression of the current item. that is, "{$ :#. news. URL} "variable expression T. item. setexpvalue ("url", newsdbprovider. getnewsurl (News); // when the news is invisible. you can cancel this output if (! News. Visible) E. Cancel = true ;}}# endregion }}

In this way, we construct a "template block parser", which can be directly used in the VT template, as shown below:

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

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

 

2. templaterendermethodattribute feature Method

The prototype of the templaterendermethodattrider feature method is similar to that of the prerender interface of the itemplaterender interface, that is, only one parameter can be provided to receive the template block object to be parsed from the vtemplate engine. The example code is as follows:

/// <Summary> /// parse the data of a template block /// </Summary> /// <Param name = "template"> </param> [templaterendermethod (description = "parse News list data")] public void rendernews (template ){

}

The VT template Code defines the rendermethod attribute of <Vt: Template>, and the defined value must be consistent with the method name in the class code, as shown in the following example code:

<Div class = "side_block">
<H3 class = "title_blue"> related news <Vt: Template Name = "topnews" type = "relating" file = "cnblogs_newsdata.html" render = "vtemplate. webtester. core. cnblogsnewsrendermethod, vtemplate. webtester "rendermethod =" rendernews "/>
</Div>
<Div class = "side_block">
<H3 class = "title_yellow"> hot news <Vt: Template Name = "topnews" type = "hoting" file = "cnblogs_newsdata.html" render = "vtemplate. webtester. core. cnblogsnewsrendermethod, vtemplate. webtester "rendermethod =" rendernews "/>
</Div>

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

Compared with the itemplaterender interface, templaterendermethodattrider has the advantage that multiple methods can be defined in a class instance to process different template blocks, that is to say, multiple "template block Resolvers" can be defined in a class instance ".

 

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.