Implementation of the template engine-ASPX page

Source: Internet
Author: User

What is a template engine? To put it bluntly, the placeholder replacement engine can be automatically replaced. In principle, there are two steps. Find-and replace it. 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 ();

The commonly used template engine, stringtemplate and velocity are almost all designed in this mode. But is this really what we need? Let's take a typical stringtemplate template.CodeLet's take a look:

First, in the template file:

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

Then, in the Code: 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 ());

 

[Nextpage]

So let's make a comparison with the ASP. NET code.

ASPX page <asp: Label id = "name" runat = "server"/> <asp: Label id = "mail" runat = "server"/>
Aspx. CS File
User US = new user ();
Us. Name = "xxsssx ";
Us. value = "80 ";
Name. Text = us. Name;
Mail. Text = us. Value

In my opinion, the template engine is needed. The separation of view and control is necessary, but we need to know what is view and what is control. Does a view not contain code or logic? The answer is no, because even if you use stringtemplate, you still need to fill it with data, the code that fills it with data is still a view, not a control, or someone simply treats it as a model. In this case, why can't we use the ASPX page itself as a template? It can be judged, can be cyclic, can be branched, and can be used with ready-made controls. For example, <%
If (helper. islogin ()){
User u = helper. getlogineduser ();
%>
Your name is: <% = U. name %>
<% }%>

 

We will compare this method with the stringtemplate method.

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

Now I may have a question: how can I call the ASPX page as a template? Why can't I generate a static page when I access it?

In fact, the solution is very simple. You only need server. Execute () to return the execution result of the ASPX page with a textwriter. We are not happy to know how to deal with this problem. Haha

I remember someone mentioned a similar method before, but some people may question: Isn't there any artist or Asp.net? In fact, if you tell the artist <% = u. name %> is the name of the user, so it does not require the artist to learn a full set of Asp.net, but in fact, it is difficult for the artist to learn simple and practical Asp.net, it may be even more depressing to learn the full template syntax of stringtemplate and to learn how to use it flexibly. IfProgramIs C # syntaxes that programmers are familiar with more approachable? In addition, it can intelligently perceive menu and compilation detection support. So I think this method is a great method in most cases.

 

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.