# Template processor

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Template_processor
> Markdown URL: https://mediated.wiki/source/Template_processor.md
> Source: https://en.wikipedia.org/wiki/Template_processor
> Source revision: 1342559172
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Software using templates to produce documents

A diagram illustrating all of the basic elements and processing flow of a template engine.

A **template processor** (also known as a **template engine** or **template parser**) is [software](/source/Software) designed to combine *template*s with data (defined by a [data model](/source/Data_model)) to produce resulting [documents](/source/Electronic_document) or [programs](/source/Computer_program).[1][2][3] The language that the templates are written in is known as a **template language** or **templating language**. For purposes of this article, a result document is any kind of formatted output, including [documents](/source/Documents), [web pages](/source/Web_page), or [source code](/source/Source_code) (in [source code generation](/source/Source_code_generation)), either in whole or in fragments. A template engine is ordinarily included as a part of a [web template system](/source/Web_template_system) or [application framework](/source/Application_framework), and may be used also as a [preprocessor](/source/Preprocessor) or [filter](/source/Filter_(software)).

## Typical features

Template engines typically include features common to most [high-level programming languages](/source/High-level_programming_language), with an emphasis on features for processing [plain text](/source/Plain_text).

Such features include:

- [variables](/source/Variable_(programming)) and [functions](/source/Function_(computer_science))

- text replacement

- file inclusion (or [transclusion](/source/Transclusion))

- conditional evaluation and [loops](/source/Control_flow#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](/source/General-purpose_programming_language), and in text processing programs, notably [text editors](/source/Text_editor) or [word processors](/source/Word_processor). 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 [‘printf’ print format strings](/source/Printf_format_string), found in many programming languages, and [snippets](/source/Snippet_(programming)), found in a number of text editors and [source code editors](/source/Source_code_editor). In word processors, [templates](/source/Template_(word_processing)) are a common feature, while automatic filling in of the templates is often referred to as [mail merge](/source/Mail_merge).

An illustrative example of the complementary nature of [parsing](/source/Parsing) and templating is the s (substitute) command in the [sed](/source/Sed) text processor, originating from search-and-replace in the [ed](/source/Ed_(text_editor)) text editor. Substitution commands are of the form s/regexp/replacement/, where regexp is a [regular expression](/source/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](/source/Escape_sequence) \1 through \9 for the *n*th 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 [relational database](/source/Relational_database), a source file such as [XML](/source/XML), an alternate format of [flat file database](/source/Flat_file_database), a [spreadsheet](/source/Spreadsheet) or any of other various sources of preformatted data. Some template processing systems are limited in the types of data that can be used. Others are designed for maximum flexibility and allow many different types of data.

### 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](/source/Pipeline_(computing)), [text file](/source/Text_file), or [stream](/source/Stream_(computing)).

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 [software application](/source/Software_application) or template engine in use. However, the flexibility of template processing systems often enables unconventional uses for purposes not originally intended by the original designers.

### 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](/source/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](/source/Web_development) using a [web template system](/source/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 [source code](/source/Source_code) (as the result documents) from abstract data models (e.g., UML, relational data, domain-specific enterprise data stores) for particular application domains, particular organizations, or in simplifying the production process for [computer programmers](/source/Computer_programmer).

### Software functionality

A [web](/source/World_Wide_Web) template engine processes web templates and source data (typically from a [relational database](/source/Relational_database)) to produce one or more output [web pages](/source/Web_page) or page fragments. It is ordinarily included as a part of a [web template system](/source/Web_template_system) or [application framework](/source/Application_framework). Currently, template processing software is most frequently used in the context of development for the [web](/source/World_Wide_Web).

## Comparison

Further information: [Comparison of web template engines](/source/Comparison_of_web_template_engines)

[XSLT](/source/XSLT) is a template processing model designed by [W3C](/source/W3C). It is designed primarily for transformations on [XML](/source/XML) data (into [web documents](/source/Web_document) or other output).

[Programming languages](/source/Programming_languages) such as [Perl](/source/Perl), [Python](/source/Python_(programming_language)), [PHP](/source/PHP), [Ruby](/source/Ruby_(programming_language)), [C#](/source/C_Sharp_(programming_language)), [Java](/source/Java_(programming_language)), and [Go](/source/Go_(programming_language)) support template processing either natively, or through add-on libraries and modules. [JavaServer Pages](/source/JavaServer_Pages),[5] [Active Server Pages](/source/Active_Server_Pages),[6] [Genshi](/source/Genshi_(templating_language)) (for Python), and [eRuby](/source/ERuby) are examples of template engines designed specifically for web application development.

Moreover, template processing is sometimes included as a sub-feature of software packages like [text editors](/source/Text_editors), [IDEs](/source/Integrated_development_environment) and [relational database management systems](/source/Relational_database_management_system).

## Benefits of using template engines

- encourages organization of source code into operationally-distinct layers (see e.g., [MVC](/source/Model%E2%80%93view%E2%80%93controller))

- 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](/source/Document_automation)

- [Document modelling](/source/Document_modelling)

- [Domain-specific programming language](/source/Domain-specific_programming_language)

- [Glossary of operating systems terms § Templating](/source/Glossary_of_operating_systems_terms#Templating)

- [Internationalization and localization](/source/Internationalization_and_localization) - [Common Locale Data Repository](/source/Common_Locale_Data_Repository) - [gettext](/source/Gettext)

- [Layout engines](/source/Layout_engines)

- [Macro (computer science)](/source/Macro_(computer_science))

- [Web template system](/source/Web_template_system) - [Comparison of web template engines](/source/Comparison_of_web_template_engines)

## References

1. **[^](#cite_ref-Niemeyer000_1-0)** Niemeyer, Patrick (2002). *Learning Java*. Sebastopol: O'Reilly. [ISBN](/source/ISBN_(identifier)) [0-596-00285-8](https://en.wikipedia.org/wiki/Special:BookSources/0-596-00285-8).

1. **[^](#cite_ref-Manolescu000_2-0)** Manolescu, Dragos (2006). *Pattern Languages of Program Design 5*. Reading: Addison-Wesley Professional. [ISBN](/source/ISBN_(identifier)) [0-321-32194-4](https://en.wikipedia.org/wiki/Special:BookSources/0-321-32194-4).

1. **[^](#cite_ref-Fowler000_3-0)** Fowler, Martin (2003). *Patterns of Enterprise Application Architecture*. Boston: Addison-Wesley. [ISBN](/source/ISBN_(identifier)) [0-321-12742-0](https://en.wikipedia.org/wiki/Special:BookSources/0-321-12742-0).

1. **[^](#cite_ref-TmpEngUses_4-0)** (see e.g., Velocity, TemplateToolkit, Freemarker ).

1. **[^](#cite_ref-JSP_for_Java_5-0)** JavaServer Pages is a technology released by Sun for use with the Java programming language. ["JavaServer Pages Technology"](http://java.sun.com/products/jsp/faq.html#1). 2006-10-10. Retrieved 2006-10-10.

1. **[^](#cite_ref-ASP10_for_VBS_JS_6-0)** ASP 1.0 was originally released for use with Microsoft VBScript and JScript. It was an extension to Microsoft IIS.

## External links

- [Enforcing Strict Model-View Separation in Template Engines](http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf)

v t e Types of computer language Types Architecture description Data exchange Hardware description Knowledge representation Markup Modeling Programming language Query Shading Specification Stylesheet Transformation See also Category:Lists of computer languages Category:Lists of programming languages List of programming languages by type

v t e Programming paradigms Imperative Structured Jackson structures Block-structured Modular Non-structured Procedural Programming in the large and in the small Design by contract Invariant-based Nested function Object-oriented Class-based, Prototype-based, Object-based Agent Immutable object Persistent Uniform function call syntax Declarative Functional Recursive Anonymous function (Partial application) Higher-order Purely functional Total Strict GADTs Dependent types Functional logic Point-free style Expression-oriented Applicative, Concatenative Function-level, Value-level Monad Dataflow Flow-based Reactive (Functional reactive) Signals Streams Synchronous Logic Abductive logic Answer set Constraint (Constraint logic) Inductive logic Nondeterministic Ontology Probabilistic logic Query Domain- specific language (DSL) Algebraic modeling Array Automata-based (Action) Command (Spacecraft) Differentiable End-user Grammar-oriented Interface description Language-oriented List comprehension Low-code Modeling Natural language Non-English-based Page description Pipes and filters Probabilistic Quantum Scientific Scripting Set-theoretic Simulation Stack-based System Tactile Templating Transformation (Graph rewriting, Production, Pattern) Visual Concurrent, parallel Actor-based Automatic mutual exclusion Choreographic programming Concurrent logic (Concurrent constraint logic) Concurrent OO Macroprogramming Multitier programming Organic computing Parallel programming models Partitioned global address space Process-oriented Relativistic programming Service-oriented Structured concurrency Metaprogramming Attribute-oriented Automatic (Inductive) Dynamic Extensible Generic Homoiconicity Interactive Macro (Hygienic) Metalinguistic abstraction Multi-stage Program synthesis (Bayesian, by demonstration, by example, vibe coding) Reflective Self-modifying code Symbolic Template Separation of concerns Aspects Components Data-driven Data-oriented Event-driven Features Literate Roles Subjects Comparisons/Lists Comparison (multi-paradigm, object-oriented, functional), List (OO, by type)

---
Adapted from the Wikipedia article [Template processor](https://en.wikipedia.org/wiki/Template_processor) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Template_processor?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
