Template engine
A template engine (of English for template or template and machine ) is a software which is a template file (Engl. Template replaced) processed and given wildcard is similar to a form by most current content. The terms template class (dt. Coll. "Template class", but factually correct: "class template") and template system are often used as a synonym for a template engine.
Class templates in C ++ programming, on the other hand, cannot be compared with template engines because theyenable programming that is independent ofthe data type andcan generateentire classes . No program code should be contained in the templates of a template engine. Control structures (such as IF) are often possible and common in templates. Typical file types that are processed are HTML , text ( ASCII or Unicode ), XML or TeX .
concept
The concept is similar to the mail merge fields in a word processor : Mail merge fields (placeholders) are inserted in a document, for example Name - Street - City . In template engines, these mail merge fields are called template variables . If several serial letters are then generated from the document, a real name such as " Erika Mustermann " appears in the completed serial letter instead of the template variable name .
The document only needs to be written and formatted once. All changes automatically affect form letters created in the future. It's the same with template engines; further parallels to serial letter templates are:
- The data come from an external source ( database , spreadsheet, etc.).
- In order to change the data, the template does not need to be changed, and vice versa.
- The template can be reused.
Demarcation
Template engines are often used by programmers to separate program code (a programming language ) from the design in the context of web applications . The manufacturers of some template engines also state that templates and engines should free the script from HTML code by separating the programming logic from the design and thus make it clearer.
This contrasts with the original idea behind the template engines: They should combine static text and dynamic content as efficiently as possible. Template engines are therefore often found in programming languages whose syntax does not directly support such a mixture (e.g. Java: JSP ; VBScript and others: ASP ). Template engines, on the other hand, are unsuitable for a real separation of the display from the data models and the logic components, and additional concepts such as model view controllers are necessary.
advantages
- Program code (PHP, Python, Ruby, Perl, JavaScript) is separated from the markup (HTML).
- Designers and programmers can work on a project at the same time.
- WYSIWYG editors can be used.
disadvantage
- Template engines produce at runtime overhead (engl. "Overhead").
- Template engines must be learned in use. In addition to the new syntax, basic knowledge of object-oriented programming is often a prerequisite.
- Template engines are limited to text output.
Language-dependent template engines
Template engines are very common. XSLT is a competition for all template engines. Smarty is widespread in PHP , but has not displaced proprietary developments and other template engines from the market.
Template engines for PHP
PHP itself is already designed in such a way that you can mix programming language constructs with the textual output using placeholders or marked areas.
<h1><?php echo $body_text; ?></h1>
<?php foreach($test_array as $key => $value) : ?>
<p>Schlüssel: <?php echo $key; ?> - Wert: <?php echo $value; ?></p>
<?php endforeach; ?>
The advantage lies in the ease of use. No additional library needs to be installed, and this procedure is easiest to understand for PHP-experienced developers and follows the principle of least surprises . It is therefore not necessary to use a separate template engine for PHP. Nevertheless, the use of a template engine for PHP offers, in addition to a better overview, some advantages, for example simple, adaptable templates for error messages and similar, recurring structures can be created. Some popular template engines are:
- Smarty
- Twig
- Fluid (as part of TYPO3 Flow )
- vlibTemplate
- TinyButStrong
- Contemplate
- ETS (Easy Template System)
- t24 - PHP template parser
- HTML_Template_IT: HTML Integrated Templates, PEAR Package uBook Template
- uBook template
- KTemplate
- Hyperkit PHP / XML template engine
- Beilpuz
- Separate
- Blade (as part of Laravel )
example
To use a template engine, you need the template file (which contains the data to be output) and the script that loads the template and fills the placeholders with values.
The template could look like this:
<body>
<p>Hallo, {name}!</p>
</body>
A value assignment from the script could look like this:
$template->assign('name', 'Erika Mustermann');
The result:
<body>
<p>Hallo, Erika Mustermann!</p>
</body>
Template engines for Perl
- Template toolkit
- Contemplate (also ASP )
- Embperl
- mason
- HTML template
- Text template
- HTML CTPP2
Template engines for Python
There are many template engines under Python . Examples are Django or TurboGears . A good overview (with URI and code examples) can be found in the German PythonWiki. A more extensive list, but less explained, can be found in the Python Wiki. Others are Cheetah, SimpleTAL, pyTemple, pyratemp and some Python template engines, Spytee, Nevow, Kid, Genshi, Jinja and HTMLTemplate.
Application example
# -*- coding: utf-8 -*-
# Modul Template muss importiert werden
from string import Template
# Vorlage zum Lesen öffnen, Inhalt übergeben und Datei schließen
vorlage = open('vorlage.txt', 'r')
vorl_inhalt = vorlage.read()
vorlage.close()
# Den Inhalt als Template behandeln
template = Template(vorl_inhalt)
# Ersetzungen nach folgendem Schema festlegen
ersetzungen = { 'Name' : 'Hans Meier', 'Straße' : 'Bahnhofstraße 69', 'Ort' : '12345 Foobarhausen' }
# Ersetzungen vornehmen
ersetzen = template.substitute(ersetzungen)
# Vorgang in Datei schreiben
outfile = open('ausgabe.txt', 'w')
outfile.write(ersetzen)
outfile.close()
template.txt:
Name: ${Name}
Straße: ${Straße}
Ort: ${Ort}
output.txt:
Name: Hans Meier
Straße: Bahnhofstr. 69
Ort: 12345 Foobarhausen
Template engines for C ++ and C
There are different template engines for C ++ and C, such as
- ClearSilver The template engine from Orkut and Yahoo Groups (use their own branch).
- Grantlee Template System
- Google CTemplate
Template engines for Java
Some template engines for Java are java-based, open source template engines.
- StringTemplate template engine by Terence Parr, basis for his ANTLR , ported to C #, Objective-C and JavaScript
- FreeMarker - Java-based template engine
- Apache Velocity - Java-based template engine
- Thymeleaf - Java-based template engine
Template engines for JavaScript
In contrast to the previous solutions, templates can be processed by the web browser. The server only delivers the data z. B. as JSON, or XML string. The rest is done by JavaScript in the browser.
- AngularJS Client-side JavaScript web framework from Google
More information on template engines
Some other template engines are Java-based:
- Hamlets - Java-based template engine
- action4JAVA - Java-based framework with template core.
- jade4j - Java-based, free implementation of the Jade template engine
- Xpand - Java-based template engine for code generation
- ASP
- CTPP, cross-platform template engine for C ++, C, PERL and PHP
- GvTags template engine for Groovy
- jQote2 - jQuery JavaScript Templating Engine
- FilesFromCSV - .NET based - creates one document per CSV data record
- Handlebars - JavaScript -based semantic template engine. a. in Ghost used
- AWS Templates Parser - Ada -based template engine
literature
- Karsten Wendland: The template cycle. Web templates in the field of tension between creative design and restrictive impertinence . Aachen 2006, ISBN 3-8322-5285-1 .
Individual evidence
- ↑ Smarty external
- ↑ Twig
- ↑ ( external ( memento of the original from January 31, 2009 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. )
- ↑ vLIB ( Memento of the original from January 21, 2005 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. : vlibTemplate ( memento of the original from June 19, 2006 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. , vlibDate ( Memento of the original from July 17, 2006 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. and vlibMimeMail ( memento of the original from July 17, 2006 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice. ( English ( Memento of the original from January 21, 2005 in the Internet Archive ) Info: The archive link has been inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this note. , German )
- ↑ TinyButStrong
- ↑ Contemplate ( Memento of the original from November 22, 2005 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ ETS (Easy Template System)
- ↑ t24 - PHP template parser
- ↑ HTML_Template_IT: HTML Integrated Templates, PEAR Package
- ↑ uBook template
- ↑ KTemplate ( Memento of the original from May 14, 2008 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ Hyperkit PHP / XML Template Engine ( Memento of the original from January 24, 2012 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ Beilpuz
- ↑ Separate
- ↑ Blade Templates - Laravel - The PHP Framework For Web Artisans. Retrieved June 1, 2019 .
- ↑ Template Toolkit
- ↑ Contemplate ( Memento of the original from November 22, 2005 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ Embperl
- ↑ Mason
- ↑ HTML template
- ↑ Text template
- ↑ HTML CTPP2
- ↑ German PythonWiki ( page no longer available , search in web archives ) Info: The link was automatically marked as defective. Please check the link according to the instructions and then remove this notice.
- ↑ English Wiki about Python
- ↑ pyratemp
- ↑ Comparison and Benchmarks: Python Template Engines
- ↑ ClearSilver
- ↑ Grantlee template system
- ↑ Project overview for Google CTemplate at Google Code
- ↑ Java-Source.net - Open Source Template Engines in Java
- ↑ Freemarker website
- ↑ Thymeleaf
- ↑ action4JAVA ( Memento of the original from February 28, 2013 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ jade4j source of supply
- ↑ oAW website
- ↑ Contemplate (English) ( Memento of the original from November 22, 2005 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ CTPP
- ↑ GvTags template engine for Groovy
- ↑ jQote2 ( Memento of the original from April 9, 2010 in the Internet Archive ) Info: The archive link was inserted automatically and has not yet been checked. Please check the original and archive link according to the instructions and then remove this notice.
- ↑ FilesFromCSV
- ↑ Handlebars.js: Minimal Templating on Steroids
- ↑ AWS Templates Parser: A template engine