Template engine development (II)-processing of value tags

Source: Internet
Author: User

I posted an article above. I will explain some comments from my friends.

My template engine is not used for MVC, but purely for the use of webpage creators. through some simple labels, the content filled in at the website background can be displayed on the foreground, generating static Web pages is similar to the idea of zhimeng CMS.

I have also studied NVelocity before. It is indeed more powerful than I have written. I studied it only to study what algorithm it uses to process documents, and why it is so fast, however, I did not study it. The current execution speed of my processing engine is only normal. It takes 20 seconds to process a single thread generated statically on the whole site. Of course, there are also reasons for the database. I used the Access database during the test. In the future, We Need To refactor and optimize the code and find a way to improve the execution speed.

 

This document describes how to process value tags.

Value tags are classified into "Global Value tags" and "local value tags". As the name suggests, Global Value tags work in the whole website template; the local value tag only works on a specific page. The two types of label styles are the same.

For example:

{% = OrgSiteName %} Name of the enterprise website, which is a global value tag

{% = Pdname %} Name of the current product, which is a local value tag

Value labels can be formatted, for example:

{% = PdPushTime: yyyy-mm-DD %} current product release time, output format: yyyy-mm-DD %}

 

The processing of value labels is very simple. The first is to retrieve the characters {% = %. I used a regular expression.

String patt = @ "{% = \ s * (\ S [^ \ s %] +) \ s * % }";

Regex rex = new Regex (patt, RegexOptions. IgnoreCase | RegexOptions. IgnorePatternWhitespace | RegexOptions. Singleline );

MatchCollection mc = rex. Matches (context );

The code above shows that the value tag can be written with spaces and is case insensitive.

The variable mc is the matched object. We can process mc cyclically. As follows:

For (int I = 0; I <mc. Count; I ++)

{

Match ma = mc [I];

// Tag in the value tag {% = tag %}

String key = ma. Groups [1]. Value. Trim ();

// Whether there is a format character

Bool isFormat = key. IndexOf (":")>-1;

String format = isFormat? Key. Substring (key. IndexOf (":") + 1 ):"";

Key = isFormat? Key. Substring (0, key. IndexOf (":"): key;

If (! IsFormat )...... // If there is no format character

If (isFormat )...... // If there is a format character

}

In the above Code, we get the specific tag in the value tag and its format character. Of course, the format character can be blank (for example, {% = orgsitename %} has no format character ).

After obtaining these two values, you can format them.

/// <Summary>
/// Format the variables. The format follows the C # format rules.
/// </Summary>
/// <Param name = "obj"> </param>
/// <Returns> </returns>
Private static string _ format (object obj, string format)
{
String val = "";
If (obj is DateTime) val = (DateTime) obj). ToString (format );
If (obj is int) val = (int) obj). ToString (format );
If (obj is long) val = (long) obj). ToString (format );
Return val;
}

Finally, replace the tag with the actual value after processing.

In the process of value tag processing, it is mainly a regular expression.

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.