Template engine is an important means to separate models from views. If you have never touched the template engine, you can refer to the introduction of Wiki. The popularity of the template engine was initially due to the needs of website development. For example, the most important template engines: smarty, velocity, and stringtemplate are all from web page design. Of course, in addition to webpage design, the template engine can also be applied to other fields, and I mainly design its application and code generator.
For the template engine, I recommend an English paper written by Terence Parr, the author of stringtemplate. Terence Parr is a university professor. The articles written are naturally academic and difficult to understand, but are of reference value. With the analysis in this article, we can find that the current template engine has two different ideas: one is to strictly separate the model and view, when designing a template system, the template language is often relatively simple, avoiding adding operators to the template language, and the other is providing powerful template language functions, the template language provides functions similar to advanced languages, such as conditional judgment statements and even mathematical computing capabilities. Obviously, from the perspective of template writers, the latter has more powerful functions and is almost omnipotent, but its security is not as good as that of the former, template writers are more likely to exploit system vulnerabilities to do things that the template system designers did not think. This is a matter of benevolence and wisdom, but many template systems can choose from.
Most template engines support Java, PHP, and python, which is of course related to the application field of the template engine. My code generator is written in C ++ and must support the Windows platform. Therefore, the selection range is limited. I searched the network for it, it seems that only Teng, CT ++, and Google ctemplate can be used. I have made a simple assessment of the three systems and actually used CT ++ and ctemplate. Now I will summarize my experiences and hope to help you.
1. Operating System Support
My main job is on Windows, and most of the template engines are for Unix/Linux, which is at a certain distance from my needs. I have downloaded Teng before, but after a long time, I have not been able to compile Teng in Visual Studio 2003, so I gave up, later, I processed CT ++ 1.8 and ran it for a while. I was very happy. Google ctemplate provides full Windows support, which is of course very worry-free for users like me.
2. Software Development Activity
These software are open-source. Of course, I am concerned about the activity of software development. Some tools are very happy when they are used at the beginning, but developers are not interested in playing, and no one takes over them, the bug cannot be updated, so it is hard to update it. A typical example is turbopower. Teng does not seem to have been updated for a long time. CT ++ has been updated, but developers are Russian. All the documents are in Russian, including program comments. In the past, 1.8 there were also English documents, I don't have it since 2.0. Although I recently promised to report English documents later than 2.4, I was worried that he would not play when he was not happy. So I finally decided to transfer it to ctemplate. Although ctemplate is Google, it is said that Google is also using it internally, but it is not very famous in the template engine field, it seems that the author's enthusiasm is still very high, continuous updates, it may soon be upgraded to Version 1.0, which gives me great confidence.
3. Functions of the template Language
In my opinion, the more functions the template language provides, the more functions it may be welcomed by template writers, but it may not comply with the strict separation of models and views. Teng and CT ++ both belong to the class with strong template language functions. For example, Teng even provides a large number of operators, while ctemplate is clearly designed in strict accordance with the principles of model and view separation, it does not even provide functions such as if/else in other template systems. As mentioned above, this question is a matter of benevolence. The wise sees wisdom and does not argue. The following is a simple comparison of tables. CT ++ 2 has never been used since it has no English documentation, and some new functions may be omitted.
|
Teng |
CT ++ |
Ctemplate |
| Variable |
Supported |
Supported |
Supported |
| Function |
Supported |
Supported |
Supported (modifier) |
| Include |
Supported |
Supported |
Supported |
| Condition Statement |
Supported |
Supported |
Not Supported |
| Loop |
Supported |
Supported |
Supported |
| Computing |
Supported |
Not Supported |
Not Supported |
| Assignment |
Supported |
Not Supported |
Not Supported |
| Note |
Supported |
Supported |
Supported |
| Security Design |
Not Supported |
Not Supported |
Supported |
| User-Defined Functions |
Not Supported |
Supported |
Supported |
4. c ++ API
The basic APIs are similar to several software. I think ctemplate is more comprehensive and I especially like its debugging function.
In general, I still had some difficulties with CT ++, but I decided to transfer it to ctemplate after comprehensive consideration.