Template engine technology in. Net -- stringtemplate (ST (or HTML) template + ASP. NET page as template)

Source: Internet
Author: User

In fact, the title is named a little big, but the subject is clear: it is to use the stringtemplate engine, using HTML pages and ASP. NET page (you can use ASP.. Net Control) as a template to implement **** (this implementation does not need to be said, Here it is omitted 200 words .)

 

 

1. Principles of template technology:

 

Is the replacement engine that can automatically replace placeholders. In principle, there are two steps. Find-> replace. However, replacement is not a simple replacement, including:

 

Simple variable replacement ();
Complex variable replacement ();
Object variable replacement ();
Key Value Type replacement ();
Replace a custom set ();
Multiple variables () are displayed simultaneously ();
Template call ();
PASS Parameters () to the calling template ();
Value template ();
Simple loop ();
Delivery cycle display ();
Template delivery cycle ();
Condition judgment ();
Create a template () from the file ();

 

Stringtemplate is basically designed in this mode.

 

 

2. Technical Implementation

 

2.1 first, in the HTML template file:

 

Name: $ user. Name $ age: $ user. value $

 

On the corresponding CS code page:

 

User US = new user (); <br/> us. name = "xxsssx"; <br/> us. value = "80"; <br/> stringtemplate ST = new stringtemplate ("$ user. name $, $ user. value $ "); <br/> St. setattribute ("user", US); <br/> console. writeline (St. tostring ());

 

 

2.2 comparison with ASP. NET pages

 

ASPX page:

 

<Asp: Label id = "name" runat = "server"/> <asp: Label id = "mail" runat = "server"/>

Aspx. CS file:

 
User US = new user (); <br/> us. name = "xxsssx"; <br/> us. value = "80"; <br/> name. TEXT = us. name; <br/> mail. TEXT = us. value

 

 

In stringtemplate, we know that stringtemplate can do a lot of work. The actual functions are very powerful, including condition judgment and loop traversal... we only need to fill in the data on the back-end code page, but what about complicated business logic? For example, we can judge whether it can be a loop, a branch, or a ready-made control. Do we expect to be able to use the powerful control that comes with ASP. NET for complex functions? OK. Congratulations! Your thoughts will be realized !! Haha !~

 

 

Before we begin, we will introduce the comparison between ASP. NET templates and stringtemplate methods:

 

Aspx template stringtemplate
The Asp.net control is not supported.
Support for complex display logic (not tested for all possibilities)
Editor support not supported
Unsupported compilation detection support

 

 

OMG !! Using ASP. NET as a template is really powerful !! But .... The key issue is .. How can I use the ASPX page as a template to be called ??

 

In fact, the solution is very simple. You only need server. Execute () to return the execution result of the ASPX page with a textwriter.

 

Server is a built-in object in ASP. It has a method called execute,UsageYes:

 

Server. Execute (PATH)

Microsoft believes that it can divide complex pages into multiple modules and then use server. Execute to display these modules on one page.

 

Example:

 

Page first. aspx:

 

<% <Br/> string STR = "this is first page! "; <Br/> response. write (STR + "started... "); <br/> server. execute ("secondpage. aspx "); <br/> response. write (STR + "end... "); <br/> %>

 

Page secondpage. aspx:

 

<% <Br/> string STR = "this is the second page! "<Br/> response. Write (STR); <br/> %>

 

Run first. aspx: The displayedResultIs:

 

This is first page! Starting... this is the second page! This is the first page! End...

 

After reading this result, have you suddenly realized it ?? Hehahaha...

But... Don't be too happy... Because we need to layout pages in the ST or HTML template pages, if the processing is not good, it will affect the layout and style of the pages, because server. execute has no return value !! So how can this problem be solved? Oh !~~

 

In fact, the solution is very simple .. As follows:

 

Textwriter Tw = new stringwriter (); <br/> httpcontext. current. server. execute ("specialtopicsdetail. aspx ", TW); </P> <p> string result = TW. tostring (); // do what you want to do ..

 

Is it simple? Hehahaha ~

 

If you want to use the user control, you can also do it. For details, refer to here:

 

Render user control as string template:

Http://aspadvice.com/blogs/ssmith/archive/2007/10/19/Render-User-Control-as-String-Template.aspx

 

 

In addition, in server. Execute, the shared content between the called and called files is:

> Application variable
> Session variable
> Server variables and attributes
> The request set and attributes, including form and querystring data.
> Response set and attributes. The called file can change the HTTP header information. However, response. Flush is not used to send messages to the client.
> Server. Execute can also form recursion, but after recursion reaches a certain number of times, it does not automatically terminate like server. Transfer, but reports an error and terminates.

 

OK. Here we only talk about the principles and implementations of the key points. In fact, we haven't introduced the basic things. I think those basic things have long been well-versed in...

I have cited many of my achievements in this article. I would like to thank them for sharing and making contributions. Thank you!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reference URL:

Http://blog.csdn.net/ming_chang/archive/2009/06/01/4234426.aspx

Http://aspadvice.com/blogs/ssmith/archive/2007/10/19/Render-User-Control-as-String-Template.aspx

Http://www.cftea.com/c/2006/09/KMA6IL9SD8Y9P6GZ.asp

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.