Template processor
A template processor (also known as a template engine or template parser) is
Typical features
Template engines typically include features common to most high-level programming languages, with an emphasis on features for processing plain text.
Such features include:
- functions
- text replacement
- file inclusion (or transclusion)
- conditional evaluation and loops
Embedded template engines
While template processors are typically a separate piece of software, used as part of a system or framework, simple templating languages are commonly included in the string processing features of general-purpose programming languages, and in text processing programs, notably text editors or word processors. The templating languages are generally simple substitution-only languages, in contrast to the more sophisticated facilities in full-blown template processors, but may contain some logic.
Simple examples include
An illustrative example of the complementary nature of
s/regexp/replacement/
, where regexp
is a regular expression, for parsing input, and replacement
is a simple template for output, either literal text, or a format string containing the characters &
for "entire match" or the special escape sequences\1
through \9
for the nth sub-expression. For example, s/(cat|dog)s?/\1s/g
replaces all occurrences of "cat" or "dog" with "cats" or "dogs", without duplicating an existing "s": (cat|dog)
is the 1st (and only) sub-expression in the regexp, and \1
in the format string substitutes this into the output.
System elements
All template processing systems consist of at least these primary elements:
- an associated data model;
- one or more source templates;
- a processor or template engine;
- generated output in the form of result documents.
Data model
This may be a
Source template
Source templates are traditionally specified:
- according to a pre-existing programming language;
- according to a specially-defined template language;
- according to the features of a hosting software application; or
- according to a hybrid combination of some or all of the above.
Template engine
The template engine is responsible for:
- connecting to the data model;
- processing the code specified in the source templates; and
- directing the output to a specific pipeline, text file, or stream.
Additionally some template engines allow additional configuration options.
Result documents
These may consist of an entire document or a document fragment.
Uses
Template processing is used in various contexts for different purposes. The specific purpose is ordinarily contingent upon the
Template engine
A template engine is a specific kind of template processing module that exhibits all of the major features of a modern programming language. The term template engine evolved as a generalized description of programming languages whose primary or exclusive purpose was to process templates and data to output text. The use of this term is most notably applied to web development using a web template system, and it is also applied to other contexts as well.[4]
Document generation
Document generation frameworks typically use template processing as the central model for generating documents.
Source code generation
Source code generation tools support generation of
Software functionality
A web template engine processes web templates and source data (typically from a relational database) to produce one or more output web pages or page fragments. It is ordinarily included as a part of a web template system or application framework. Currently, template processing software is most frequently used in the context of development for the web.
Comparison
Moreover, template processing is sometimes included as a sub-feature of software packages like
Benefits of using template engines
- encourages organization of source code into operationally-distinct layers (see e.g., MVC)
- enhances productivity by reducing unnecessary reproduction of effort
- enhances teamwork by allowing separation of work based on skill-set (e.g., artistic vs. technical)
See also
- Document automation
- Document modelling
- Domain-specific programming language
- Glossary of operating systems terms § Templating
- Internationalization and localization
- Layout engines
- Macro (computer science)
- Templates
- Template systems
- Web template system
References
- ISBN 0-596-00285-8.
- ISBN 0-321-32194-4.
- ISBN 0-321-12742-0.
- ^ (see e.g., Velocity, TemplateToolkit, Freemarker ).
- ^ JavaServer Pages is a technology released by Sun for use with the Java programming language. "JavaServer Pages Technology". 2006-10-10. Retrieved 2006-10-10.
- ^ ASP 1.0 was originally released for use with Microsoft VBScript and JScript. It was an extension to Microsoft IIS.