Creating your own template engine using asp.net is quite simple, and we only need the Regular Expression and reflection tools.
Let's take a look at the following template page from a real project (simplified for ease of reading ):
<! DOCTYPE html>
On this template page, $ method (param) is used to call the method provided by data. A template fragment parameter can be accepted within the method, and {paramName} is used for parameter modification, and return data.
Through the file, we can easily see that this is a blog post display page, including the comment function. Through a slot combination, We can modify this file without rewriting the code (only for systems with relatively fixed functions) and make full use of the convenient advantages of the template. The next series of articles will show you how to develop a custom template engine through analysis of this template engine ....
The following code snippet: SmpleTpl. cs
/*************************************** * ***** File Name: simpleTpl. cs * file Description: asp.net template engine parsing file * created by: newmin * personal blog: *************************************** * **/namespace U1City. shop. unit {using System; using System. collections. generic; using System. linq; using System. text; using System. text. regularExpressions; using System. reflection; /// <summary> /// simple template parsing class /// </summary> public sealed class SimpleTpl {/// <summary> /// type instance containing the method/ /// </summary> private object classInstance; public SimpleTpl (object classInstance) {this. classInstance = classInstance ;} /// <summary> // data column regular // </summary> private static Regex fieldRegex = new Regex ("{([a-z0-9 _] + )}"); /// <summary> /// execute the parsing template content /// </summary> /// <param name = "html"> </param> /// <returns> </returns> public static string Execute (object instance, string html) {string resultTxt = html; // return result const string tagPattern = "\\$ ([a-z0-9] +) \ ([^)] + ') \) "; const string paramPattern =" '([^'] +) ', * "; Regex tagRegex = new Regex (tagPattern ); // method Regular Regex paramRegex = new Regex (paramPattern); // parameter regular Type = instance. getType (); MethodInfo method; string tagName; object [] parameters; Type [] parameterTypes; // parameter Type array MatchCollection paramMcs; resultTxt = tagRegex. replace (resultTxt, m => {tagName = m. groups [1]. value; string x = m. groups [2]. value; // obtain the parameter paramMcs = paramRegex. matches (m. groups [2]. value); parameters = new object [paramMcs. count]; // check whether a method exists (the method parameters are of the string Type) parameterTypes = new Type [parameters. length]; for (int I = 0; I <parameterTypes. length; I ++) {parameterTypes [I] = typeof (String);} method = type. getMethod (tagName, BindingFlags. instance | BindingFlags. nonPublic | BindingFlags. ignoreCase, null, parameterTypes, null); // if the method exists, the execution returns the result. Otherwise, the original value if (method = null) {return m. value;} else {// assign a Value to the parameter array for (int I = 0; I <paramMcs. count; I ++) {parameters [I] = paramMcs [I]. groups [1]. value;} // executes the method and returns the return method. invoke (instance, parameters ). toString () ;}}); return resultTxt ;}/// <summary >/// Execute the parsing template content /// </summary> public string Execute (string html) {return Execute (this. classInstance, html );}}}