A Simple Web Page Template Parser And A Template Pool

原创 2004年08月24日 01:58:00

Sample Image

Introduction

ASP.NET provides us with many easy ways to build our web system, especially the code-behind technique which amazingly allows a separation of layout and code. However, ASP.NET also offers some mechanisms to allow you to build a custom programming model more than that offered by code-behind. One mechanism is HTTP Handler which gives you a means of interacting with the low-level request and response services of the IIS Web server, and provides functionality much like ISAPI extensions but with a simpler programming model. Great! This mechanism is just what I do like most, because it gives me a nice feeling that everything is under my own control and I'm free.

But when you are writing custom HTTP handlers, hard-coding the page layout is boring and error-prone. We do need a way to separate layout from code. Thus, class TmplParser, TemplatePool etc. were born. The main job of class TmplParser is to parse a layout template file with some tags and labels whose rules are simple and defined by myself :-). The class TemplatePool is used to buffer a set of templates, which can reduce the I/O operations with less template file reading, and improve the performance. I will illustrate how to use them to separate layout from code, later in this article.

Notes: It's the first time that I programmed ASP.NET, the first time that I programmed in C#, the first time that I touched IIS, the first time that I submitted an article to Code Project. So, there must be some problems or something not good enough in the article and code. And I do welcome any feedback. Thanks!

Using the code

First, I will tell you the rules of using the parser and some basic information as well.

  1. A template file consists of two and only two basic elements. They are block and label. A block is defined with begin-flag <!--BEGIN: BLOCKNAME --> and end-flag <!--END: BLOCKNAME -->. A label is defined as {LABELNAME}. Let's look at an example template file example.html to make the concepts more clear to you.

    example.html

    <html>
    <head>
    <title> Example </title>
    </head>
    <body>
    <table>
      <!--BEGIN: FORMAT1 -->
      <tr><th>{THEAD1}</th></tr>
      <!--BEGIN: ROW1 -->
      <tr><td>{VALUE1}</td></tr>
      <!--END: ROW1 -->
      <!--END: FORMAT1 -->
      <!--BEGIN: FORMAT2 -->
      <tr><th>{THEAD1}</th><th>{THEAD2}</th></tr>
      <!--BEGIN: ROW2 -->
      <tr><td>{VALUE1}</td><td>{VALUE2}</td></tr>
      <!--END: ROW2 -->
      <!--END: FORMAT2 -->
    </table>
    </body>
    </html>

    In the template file example.html, there are four blocks: FORMAT1, ROW1, FORMAT2 and ROW2. Block FORMAT1 has one label: THEAD1; block ROW1 has one label: VALUE1; block FORMAT2 has two labels: THEAD1 and THEAD2; block ROW2 has two labels: VALUE1 and VALUE2. FORMAT1 and FORMAT2 are parent blocks of ROW1 and ROW2 respectively. Actually, there is one more block. It's the root block, namely the template file itself. Let's call it DOCUMENT-BLOCK.

  2. Rules of Template File Definition
    • A block name must be unique in one template file. It's not only for code readability but also for easy use. And my principle is: the simpler, the better.
    • A block can be a parent by enclosing other blocks. But any two blocks can not overlap. And there is no sibling relation, but parent-children relation is maintained between blocks.
  3. A Deep Look

    When the template file is parsed by TmplParser as the following code:

    // tmplDir is the path of the directory in which example.html is placed.
    // It's in the format like this: E:/Demo/tmpl/
    // 50 is the capacity of the pool.
    // The pool uses LRU algorithm for template replacement.
    TemplatePool.Singleton(tmplDir, 50);
    
    // What GetTemplate does is to load the template file,
    // create and initialize a new instance
    // of TmplParser for this template file,
    // do the parsing and return it if the filename
    // passed is not found in the pool,
    // otherwise just return a clone
    // of the instance found in pool.
    // It's thread-safe.
    // ITemplate is an interface inherited by TmplParser.
    ITemplate tmpl = TemplatePool.Singleton().GetTemplate("example.html");
    
    // the second time, just return a cloned instance
    // for template example.html
    // The code below is just for instruction.
    ITemplate tmpl2 = TemplatePool.Singleton().GetTemplate("example.html");

    Then five BlockParser (a private class nested in TmplParser) instances will be built and maintained in TmplParser. The contents of the five blocks are below:

    ROW1

      <tr><td>{VALUE1}</td></tr>

    ROW2

      <tr><td>{VALUE1}</td><td>{VALUE2}</td></tr>

    FORMAT1

      <tr><th>{THEAD1}</th></tr>
      <tag:ROW1/>

    FORMAT2

      <tr><th>{THEAD1}</th><th>{THEAD2}</th></tr>
      <tag:ROW2/>

    DOCUMENT-BLOCK

    <html>
    <head>
    <title> Example </title>
    </head>
    <body>
    <table>
      <tag:FORMAT1/>
      <tag:FORMAT2/>
    </table>
    </body>
    </html>

    Once a child block (example: ROW1) is Out, its current content will be placed just before the tag <tag:BLOCKNAME/> (example: <tag:ROW1/>) in its parent block and it will return to the original raw content.

    After the following code is executed, the content of DOCUMENT-BLOCK will be ...

    ITmplBlock tmplROW1 = tmpl.ParseBlock("ROW1");
    ITmplBlock tmplFORMAT1 = tmpl.ParseBlock("FORMAT1");
    
    //- 1: child block(ROW1) is not Out
    // Replace the label {THEAD1} in block FORMAT1 with OS.
    tmplFORMAT1.Assign("THEAD1", "OS");
    
    // Replace the label {VALUE1} in block ROW1 with WinXP.
    tmplROW1.Assign("VALUE1", "WinXP");
    
    // Be careful, the code below is commented deliberately.
    // So block ROW1 content won't be embeded into its parent block FORMAT1.
    // tmplROW1.Out();
    
    tmplFORMAT1.Out();
    
    //- 2: parent block(FORMAT1) is Out before child block(ROW1)
    // you won't see the ROW1 content in the result either.
    tmplFORMAT1.Out();
    tmplROW1.Assign("VALUE1", "WinXP");
    tmplROW1.Out();
    
    //- 3: Replace the child block's label in the parent
    //     block by outing child block first
    // It does work.
    tmplROW1.Out();
    tmplROW1.Out();
    tmplFORMAT1.Assign("THEAD1", "Tools");
    // {VALUE1} is a label in block ROW1.
    tmplFORMAT1.Assign("VALUE1", "Visual Studio .NET");
    
    //- 4: General usage
    ITmplBlock tmplROW2 = tmpl.ParseBlock("ROW2");
    ITmplBlock tmplFORMAT2 = tmpl.ParseBlock("FORMAT2");
    
    tmplFORMAT2.Assign("THEAD1", "Country");
    tmplFORMAT2.Assign("THEAD2", "City");
    tmplROW2.Assign("VALUE1", "China");
    tmplROW2.Assign("VALUE2", "Pekin");
    tmplROW2.Out();
    tmplROW2.Assign("VALUE1", "China");
    tmplROW2.Assign("VALUE2", "Shanghai");
    tmplROW2.Out();
    tmplROW2.Assign("VALUE2", "Tianjin");
    tmplROW2.Out();
    tmplROW2.Assign("VALUE2", "Chongqing");
    tmplROW2.Out();
    tmplROW2.Assign("VALUE2", "Shenzhen");
    tmplROW2.Out();
    tmplFORMAT2.Assign("VALUE1", "China");
    tmplFORMAT2.Out();
    
    // Calling the method ParseBlock, the one without
    // parameters, can get DOCUMENT-BLOCK.
    ITmplBlock tmplDoc = tmpl.ParseBlock();
    tmplDoc.Out();
    
    // the next step will usually be like the code below
    // which just sends the result content to the client.
    Response.Write(tmplDoc.BlockString);

    Then the result content of DOCUMENT-BLOCK is shown as follows:

    <html>
    <head>
    <title> Example </title>
    </head>
    <body>
    <table>
      <!-- 1 -->
      <tr><th>OS</th></tr>
      <!-- 2 -->
      <tr><th>{THEAD1}</th></tr>
      <!-- 3 -->
      <tr><th>Tools</th></tr>
      <tr><td>Visual Studio .NET</td></tr>
      <tr><td>Visual Studio .NET</td></tr>
      <!-- 4 -->
      <tr><th>Country</th><th>City</th></tr>
      <tr><td>China</td><td>Pekin</td></tr>
      <tr><td>China</td><td>Shanghai</td></tr>
      <tr><td>China</td><td>Tianjin</td></tr>
      <tr><td>China</td><td>Chongqin</td></tr>
      <tr><td>China</td><td>Shenzhen</td></tr>
    </table>
    </body>
    </html>

    The comments in the result content are added just for your convenience to contrast to the C# code above. They don't exist in the real result content. As you all see, the presentation code (HTML) can be reused much with this technique.

Now, I believe that you have known much about this technique, which really will makes me happy :-). One point should be accentuated. That is: with one block's Out method being not called, its content won't be placed in its parent.

Second, I will illustrate how to use the code with a simple demo project. Actually, it's a framework shown with the template parser and pool more than a usage instruction. However, I'll just list the main code. Details should be viewed in the source code by yourself. All the source code can be downloaded through the link above.

  1. Build a web site called Demo whose virtual directory is the one that you extract the demo project's source files to. Below I will use $DEMO to refer to this virtual directory.
  2. In the IIS, disable all the access privilege of the directory $DEMO/tmpl under which our web page template files are placed, and $DEMO/src under which our source code files are placed. It just prevents clients from accessing to any resources under the two directories in any way.
  3. View the config file web.config. Each request whose path matches *do.aspx will be handled by Demo.Handler.Controller. $DEMO is the absolute path of your website virtual directory. For example, if your virtual directory is E:/MyWebsite/demo, then the config below should be:
    <add key="tmpldir" value="E:/MyWebsite/demo/tmpl/"/>
    <configuration>
      <system.web>
        ...
        <httpHandlers>
          <add verb="*" path="*do.aspx" type="Demo.Handler.Controller, demo"/>
    
        </httpHandlers>
        ...
      </system.web>
        
      <appSettings>
        <add key="tmpldir" value="$DEMO/tmpl/"/>
        <add key="capacity" value="50"/>
      </appSettings>  
    </configuration>
  4. The following code in the file Global.asax.cs is to create a single instance of TemplatePool with the singleton pattern.
    public class Global : System.Web.HttpApplication
    {
        ...
    
        //
        // application's initialization
        //
        protected void Application_Start(Object sender, EventArgs e)
        {
            TemplatePool.Singleton(
              ConfigurationSettings.AppSettings.Get("tmpldir"),
              Int32.Parse(ConfigurationSettings.AppSettings.Get("capacity")));
        }
        ...
    }

    What TemplatePool.Singleton does is shown below. As you all see, it is thread-safe and uses the double-check trick to improve the performance. And the pool instance will exist through the process life.

    public sealed class TemplatePool : ITmplLoader
    {
        ...
        public static TemplatePool Singleton(string tmplDir, int capacity)
        {
            if(pool == null)
            {
                lock(objLock)
                {
                    if(pool == null)
                        pool = new TemplatePool(tmplDir, capacity);
                }
            }
            return pool;
        }
        ...
        private static TemplatePool pool = null;
        private static object objLock = new object();
    }
  5. View class Demo.Handler.Controller to see how the handler deals with the request.
    public class Controller : IHttpHandler, IRequiresSessionState
    {
     public void ProcessRequest(HttpContext context) 
     {
      ...
      // A more sophisticated way is to put the info,
      // such as Demo.Application.MenuDealer, menupanel.html etc,
      // into a config(an XML file or a table in db or others).
      IApplication theApp = (IApplication) 
       Activator.CreateInstance(Type.GetType("Demo.Application.MenuDealer"));
      theApp.Init("menupanel.html", "login.html");
      theApp.Session = context.Session;
      theApp.DoProcess(context.Request.Params);
      context.Response.Write(theApp.Out);
     }
     ...
    }
  6. View class Demo.Application.MenuDealer to see what method DoProcess does. It just composites the data with the template to generate a string to be responded.
    public class MenuDealer : Dealer
    {
        ...
        // generate menus
        string oldModName = "";
        string modname = null;
        string username = reqParams.Get("username");
    
        Hashtable htMenu = null;
    
        ITmplBlock tmplMenuFrm = OutPageTmpl.ParseBlock("MENUFRM");
        ITmplBlock tmplMenu = OutPageTmpl.ParseBlock("MENU");
    
        int modIdx = 0;
    
        Operation operation = new Operation();
    
        operation.GetMenuStart();
        string action = "do.aspx?opname=";
    
        while((htMenu = operation.GetMenuNext()) != null)
        {
            modname = (string)htMenu["modname"];
    
            if(!modname.Equals(oldModName))
            {
                if(modIdx > 0)
                tmplMenuFrm.Out();
    
                tmplMenuFrm.Assign("IDIDX", modIdx.ToString());
                tmplMenuFrm.Assign("MODULE", modname);
    
                oldModName = modname;
                modIdx++;
            }
    
            string opname = (string)htMenu["opname"];
            tmplMenu.Assign("REQUEST", opname + action + opname);
            tmplMenu.Assign("MENU", (string)htMenu["opvalue"]);
            tmplMenu.Out();
        }
    
        if(modIdx > 0)
            tmplMenuFrm.Out();
    
        tmplDoc = OutPageTmpl.ParseBlock();
        tmplDoc.Assign("USERNAME", username);
        ...
    }
  7. View class Demo.Application.Dealer to see what the properties Out and OutPageTmpl do.
    public abstract class Dealer : IApplication
    {
        ...
        public string Out
        {
            get
            {
                if(tmplDoc == null)
                    return null;
                tmplDoc.Out();
                return tmplDoc.BlockString;
            }
        }
    
        protected ITemplate GetTemplate(string tmplFileName)
        {
            return (ITemplate)
              (TemplatePool.Singleton().GetTemplate(tmplFileName));
        }
    
        protected ITemplate OutPageTmpl
        {
            get
            {
                if(outPageTmpl == null)
                    outPageTmpl = GetTemplate(outPage);
                return outPageTmpl;
            }
        }
        ...
    }

At last, build the system with Visual Studio .NET, and you will see what the image above shows. And I believe you will find more useful code in the demo project's source code. As I already said, the demo project is just a framework for a small project. May you like it!

Points of Interest

  1. When I implement class TemplatePool, I need a class for linked list. But, I can't find one in the namespace System.Collections. There may be someone shouting why not use ArrayList. Good question! But I guess that ArrayList will do the elements copying to move their positions when operations Insert and Remove are being done, which does raise performance penalty when the two operations are done frequently in LRU algorithm. So, I implemented a simple double linked list Agemo.Utility.DoubleLinkedList to meet my needs. It's really simple. Again, the simpler, the better.
  2. The non-recursive DFS algorithm is used to disassemble the template into blocks.
  3. The HttpHandler class must inherit the interface IRequiresSessionState which is just a marker interface if it will use the session object. I was harassed for a long time by this problem.

Agemo


Click here to view Agemo's online profile.

QT工程pro文件TEMPLATE变量说明

QT工程pro文件模板变量(TEMPLATE) 模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择: app - 建立一个应用程序的makefile。这是默认值...
  • qinpanke
  • qinpanke
  • 2014年03月26日 18:00
  • 5368

C++中 模板Template的使用

1、在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 答:class用于定义类,在模板引入c++后,最初定义模板的...
  • liyuan_669
  • liyuan_669
  • 2014年08月12日 18:07
  • 2754

Odoo Page页结构

Odoo page的结构 一个Odoo页面(page)是一个组合的cross-pages和unique两种元素的视觉效果。默认情况下,Odoo为你提供了一个Header和Footer(cross-p...
  • Pompeii
  • Pompeii
  • 2017年07月13日 21:20
  • 293

h5学习笔记:weui text/template 使用

昨天开始尝试一下使用这个,之前使用weui的时候,产生一个列表。通过ajax 获取一个数组列表。这个时候,第一次做的时候采用字符串拼凑的方式将字符串拼凑起来,在当时而言这种做法凑效,但相当痛苦。Vue...
  • hero82748274
  • hero82748274
  • 2017年04月07日 23:17
  • 2640

js中模板化开发,text/template

JavaScript标签中有时候会看见,大概就是一个放置模板的地方,而这些东西并不显示在页面,我们暂且就把它当成一个容器.百度查了一下也没查到怎么个用法,所以自己就写了一下代码,以便应用这些模板内容。...
  • ThirteenFloor
  • ThirteenFloor
  • 2017年02月14日 13:50
  • 5394

C++模板template用法总结

引言 模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计。C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream。 ...
  • qq_35637562
  • qq_35637562
  • 2017年02月15日 12:37
  • 4312

样式和模板快速入门Style,Template

http://www.cnblogs.com/jv9/archive/2010/04/14/1711520.html   样式(Style)和模板(Template)的定义 ...
  • swarb
  • swarb
  • 2013年12月18日 14:51
  • 869

web.py使用模板时报错No template named index原因及解决方案

初次接触web.py时,大多会接触到web.py的官方中文教程(http://webpy.org/docs/0.3/tutorial.zh-cn),在运行其中包含的一个关于模板(template)的示...
  • sunruxiaosrx
  • sunruxiaosrx
  • 2016年03月09日 10:29
  • 1778

模板(template)中typename的使用方法

声明template参数时, 前缀关键字class和typename可以互换; 使用关键字typename标识嵌套从属类型名称, 但不需在基类列表和成员初始化列表内使用. 从属名称(depen...
  • darennet
  • darennet
  • 2014年11月09日 16:10
  • 18301

C++模板(template)使用介绍

1. 模板的概念。我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同。正确的调用重载函数。例如,为求两个数的最大值,我们定义MAX()函数需...
  • chenchen410
  • chenchen410
  • 2014年09月23日 18:44
  • 33422
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:A Simple Web Page Template Parser And A Template Pool
举报原因:
原因补充:

(最多只允许输入30个字)