ASP. NET regular template engine code

Source: Internet
Author: User

We declare an array

    Public Static New regex[];

The next key regular expression:

RegexOptions options =Regexoptions.none; //Nested template tags (compatible)r[0] =NewRegex (@"<!--{Template (skin=\\ "" ([^\[\]\{\}\s]+) \ \ "" (?: \ s+))?) src= (?: \ /|\\ "") ([^\[\]\{\}\s]+) (?: \ /|\\"")(?:\ s*)}-->", Options); //template path Label (new)r[1] =NewRegex (@"<!--{Templateskin (= (?: \ \ "") ([^\[\]\{\}\s]+) (?: \ \""))?) (?:\ s*)}-->", Options); //namespace Tagsr[2] =NewRegex (@"<!--{namespace (?: ""?) ([\s\s]+?) (?:""?)} -", Options); //C # code tagsr[3] =NewRegex (@"<!--{csharp}--> ([\s\s]+?) <!--{/csharp}-->", Options); //Loop Loop (discard)r[4] =NewRegex (@"<!--{Loop (([^\[\]\{\}\s]+) \)?) ([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+)} -", Options); //foreach Loop (new)r[5] =NewRegex (@"<!--{foreach (?: \ s*) \ (([^\[\]\{\}\s]+) ([^\[\]\{\}\s]+) in ([^\[\]\{\}\s]+) \) (?: \ s*)}-->", Options); //For Loop (new)r[6] =NewRegex (@"<!--{for\ ([^\ (\) \[\]\{\}]+) \) (?: \ s*)}-->", Options); //If statement label (discard)r[7] =NewRegex (@"<!--{if (?: \ s*) ([^\s]+) ((?: \ s*) (\|\| | \&\&) (?: \ s*) ([^\s]+))?) (?:\ s*)}-->", Options); r[8] =NewRegex (@"<!--{Else ((?: \ s*) if (?: \ s*) ([^\s]+) ((?: \ s*) (\|\| | \&\&) (?: \ s*) ([^\s]+))) (?:\ s*)}-->", Options); //if Statement label (new)r[9] =NewRegex (@"<!--{if\ (([^\s]+) ((?: \ s*) (\|\| | \&\&) (?: \ s*) ([^\s]+))?) \)(?:\ s*)}-->", Options); r[Ten] =NewRegex (@"<!--{Else ((?: \ s*) if\ (([^\s]+) ((?: \ s*) (\|\| | \&\&) (?: \ s*) ([^\s]+))?) \))(?:\ s*)}-->", Options); //Loop and end of Judgment label (compatible)r[ One] =NewRegex (@"<!--{\/(?: Loop|foreach|for|if) (?: \ s*)}-->", Options); //Continue labelr[ A] =NewRegex (@"<!--{continue (?: \ s*)}-->"); //Break labelr[ -] =NewRegex (@"<!--{break (?: \ s*)}-->"); //Request Labelr[ -] =NewRegex (@"(\{request\[([^\[\]\{\}\s]+) \]\})", Options); //Intercept String Tagsr[ the] =NewRegex (@"(<!--{cutstring\ ([^\s]+?), (. \d*?) \)}-->)", Options); //URL link Labelr[ -] =NewRegex (@"(<!--{linkurl\ ([^\s]*?) \)}-->)", Options); //declaring assignment Labels (compatible)r[ -] =NewRegex (@"<!--{set (\ (? ( [\w\.<>]+] (?: \) | ))?) (?:\ s*) \{? ( [^\s\{\}]+] \}? (?:\ s*) = (?: \ s*) (. *?) (?:\ s*)}-->", Options); //Data Variable Labelsr[ -] =NewRegex (@"(\{([^\[\]\{\}\s]+) \[([^\[\]\{\}\s]+) \]\})", Options); //Common Variable Tagsr[ +] =NewRegex (@"({([^\[\]/\{\}=: ' \s]+)})", Options); //time format conversion labelr[ -] =NewRegex (@"(<!--{datetostr\ ([^\s]+?), (. *?) \)}-->)", Options); //Integer Conversion labelr[ +] =NewRegex (@"(\{strtoint\ ([^\s]+?) \)\})", Options); //Direct Output Labelr[ A] =NewRegex (@"<!--{(?: Write |=) (?: \ s*) (. *?) (?:\ s*)}-->", options);

Look at a bunch of them! It's hard to feel like it's not going to be the main.

Now we're going to use the following method in the main talk about the Replace the statement if label

Stringstrtemplate= ""//Place the template content you want to replaceforeach(Match minchr[7]. Matches (strtemplate)) {Iscodeline=true; Strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\tif ("+ m.groups[1]. ToString (). Replace ("\\\"","\"") +") \r\n\t{"); }            foreach(Match minchr[8]. Matches (strtemplate)) {Iscodeline=true; if(m.groups[1]. ToString () = =string. Empty) {strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\t}\r\n\telse\r\n\t{"); }                Else{strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\t}\r\n\telse if ("+ m.groups[3]. ToString (). Replace ("\\\"","\"") +") \r\n\t{"); }            }            foreach(Match minchr[9]. Matches (strtemplate)) {Iscodeline=true; Strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\tif ("+ m.groups[1]. ToString (). Replace ("\\\"","\"") +") \r\n\t{"); }            foreach(Match minchr[Ten]. Matches (strtemplate)) {Iscodeline=true; if(m.groups[1]. ToString () = =string. Empty) {strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\t}\r\n\telse\r\n\t{"); }                Else{strtemplate= Strtemplate.replace (m.groups[0]. ToString (),"\r\n\t}\r\n\telse if ("+ m.groups[3]. ToString (). Replace ("\\\"","\"") +") \r\n\t{"); }            }            

Writing a template engine yourself is a hassle, and it may be easier to make direct dynamic pages and pseudo-static. It used to be a velocity template engine, and it was good to use.

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.