Easy to understand: develop a template engine by yourself-what is a template engine?

Source: Internet
Author: User

At the invitation of the brothers in the group, Chen finally decided to share the template engine development technology under. net. This seriesArticleIt will give you a full understanding of the concept, design, analysis, and practical application of the template engine, and develop your own template engine step by step. About the concept of the template engine, I entered my own explanation on Baidu encyclopedia last year (refer to: template engine ). Chen once developed a setNetbird ASP. NET template engineAlthough I am not happy to promote it myself, it cannot block the love of friends!

The original template engine was designed to implement business logic andCodeSeparation, and later found that this mode brings great convenience to interface maintenance, which is also an implementation of MVC mode. Now we can see the template engine in many places, such as various CMS, forums, code generators, and even JavaScript libraries such as mootools and jquery. So what exactly is the template engine? What is the significance of the template engine in the actual production environment? Should we use the template engine technology? Next we will answer them one by one.

What is the template engine?

The template engine is designed to separate the business logic layer from the presentation layer and convert the template code in the specified format to the business dataAlgorithm. It can be a process code, a class, or even a class library. Different template engines have different functions, but their basic principles are similar.

The basic mechanism of the template engine is to replace (convert), convert the specified tag to the required business data, and output the specified pseudo statement according to a certain process. The minimum template engine is similar to the following code:

1 Const StringMessage ="{Person} like {color}";
2
3 VaRMSG = message. replage ("{Person}", Person). replage ("{Color}", Color );

In fact, we have unconsciously used the template mechanism in many places during the development process. But for the above Code, there is a better string. Format () in C #, and it also implements a template engine.

According to the specific implementation method, the template engine is divided into three basic types:

    1. The replacement template engine.Replacement is to replace the specified text mark with the target content. This template engine is easy to implement. In addition to tag replacement, it rarely supports sub-template reference and process control. As mentioned above, we use this template engine almost every day. The replacement template engine is usually used for projects with less complex business logic, such as generating text messages, generating emails, and even using it by many code generators. It can be said that the idea of the replace template engine is the foundation of the entire template engine field.
    2. Interpreted template engine.The principle of the interpreted template engine is Mark replacement. The interpreter can support more complex tags and syntaxes. We will introduce a very useful template engine implemented using the reflection mechanism later. As you all knowProgramming LanguageIn, interpreted and compiled languages are much inferior to each other, depending on how the interpreter is optimized.
    3. Compile template engine.The compiled template engine is relative to the interpreted template engine. In fact, it is not suitable to be named as the compiled template engine, but I did not expect any other good name! The compiled template engine we will introduce is dedicated to ASP. NET Website development. It converts the template we define into A. aspx file through the parsing engine and a series of algorithms, and then directly accesses the. aspx file.
Template engine application

In many cases, the template engine is designed to implement code separation between the business logic layer and the presentation layer for collaborative development. However, there are also many applications, such as code generators. Code separation here is actually to achieve better scalability.

ASIDE: In the early days, only webform was the web development model that. Net admired. I personally transferred from ASP/PHP to. net. In the ASP era, I developed my own template engine, not to mention the PHP environment. The open-source frameworks with a template engine are also called idea. It can be said that in addition to. net, monkeys in other camps are actually no stranger to the template engine. Later, Microsoft launched the MVC framework, and I have seen its implementation. The basic idea is to bring back the ASP encoding method in the past, which is not convenient enough in many places. I still stick to the use of the template engine.

The template engine can implement code separation and allow developers to perform their respective jobs. It is also one of the basic components of almost all code generators. With the template engine, the template engine is used in the presentation layer and final business data, we can have better scalability.

Better template engine implementation, such as Lao Chen's "net bird ASP. NET template engine", and built-in template code reuse, multi-language support, dynamic page and Static Page Automatic Balancing and other functions.

Summary

This article briefly describes the concept of the template engine, which is extremely useful. Starting from the next article, we will explore in depth the development technologies and algorithms required to implement various template engines.

As this series of articles are not systematically planned at the time of writing, it is possible to add or delete chapters as appropriate. All in all, the template engine seems to be a small thing, but it will seem quite complicated and huge to be done from the ground up. Be prepared! I will try my best to give a brief introduction.

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.