繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 评论及其它 >> A Simple Web Page Template Parser And A Template Pool

A Simple Web Page Template Parser And A Template Pool

2005-01-09 20:51:55  作者:foxmail  来源:互联网  浏览次数:1  文字大小:【】【】【
简介: Download demo project - 27.4 Kb Download source - 5.09 Kb Introduction ASP.NET provides us with many easy ways to build our web system, especially the code-behind technique which amazingl...

Download demo project - 27.4 Kb

Download source - 5.09 Kb

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.

A template file consists of two and only two basic elements. They are block and label. A block is defined with begin-flag and end-flag . 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

Example

{THEAD1}
{VALUE1}
{THEAD1}{THEAD2}
{VALUE1}{VALUE2}

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.

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.

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.

责任编辑:admin
相关文章