What is a template engine

Source: Internet
Author: User
Tags php template

What the template engine is.

The template engine is based on the separation of the business logic layer and the presentation layer, and transforms the template code of the specified format into the algorithm implementation of the business data.

It can be a process code, a class, or even a class library. The different template engines function differently, but their basic principles are similar.

The basic mechanism of the template engine is to replace (transform), to convert the specified label to the required business data, and to transform the specified pseudo statement according to a process.

The minimum template engine is similar to the implementation of the following code:

1 Const String message = ' {person} likes ' {Color} ';

2 var msg = message. Replage (' {person} ', person). Replage ("{color}", color);

In fact, we have been unconsciously using the template mechanism in many places in the development process. Just for the code above,

In C # There is a better string.format () that can be used, and it also implements a set of template engines.

In a specific way, I divided the template engine into three basic types:

Displacement template engine. Replacement is to replace the specified text tag with the target content. This template engine is simple to implement, with little support for such as child template references, process Control, and so on, in addition to tag substitution. As mentioned above, we use this template engine almost every day. The replacement template engine is often used in projects where business logic is not very complex, such as generating text messages, generating e-mail messages, and even many code generators are using it. It can be said that the concept of replacement template engine is the basis of the entire template engine industry.

Interpretative template engine. The rationale for the interpretative template engine is still a markup substitution. It is only the presence of an interpreter that can support more complex markup and syntax. Later we will introduce a very useful template engine that is implemented using the reflection mechanism. As we all know, in the programming language, the explanatory language and the compiled language are much worse than the line, which depends largely on how the interpreter optimizes.

The compile-template engine. The compiled template engine is relative to the interpretative template engine, and it's actually not the right name for the compile-template engine, but I didn't think of any other good names, ha. The compiled template engine we will be introducing is dedicated to asp.net web development. It transforms our defined template into an. aspx file by parsing the engine and a series of algorithms, and then accesses the. aspx file directly.

Application of template engine

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

Smarty

Smarty is a PHP development based PHP template engine. It provides a separation of logic from external content, simply speaking, the goal is to use the PHP programmer with the Art of separation, the use of programmers to change the logic of the program will not affect the design of the page, the art of modifying the page will not affect the program logic, which in many people cooperation project is particularly important.

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.