A template engine is the software component that turns a template — a document with placeholders, loops, and conditionals — plus a set of data into a final rendered output. In web development the output is usually an HTML page, but the same idea drives email generation, configuration-file generation, and code generation. The engine keeps the static structure of a document in one place and fills in the variable parts at render time, which is the practical mechanism behind server-side rendering.
The pattern enforces a separation of concerns between presentation and logic. Django’s template documentation describes a template as “a text file” that “can generate any text-based format (HTML, XML, CSV, etc.)” and explains that a template contains variables, “which get replaced with values when the template is evaluated, and tags, which control the logic of the template.” Jinja, the engine behind Flask and many other Python tools, describes templates similarly as containing “variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.” The author writes markup with holes; the engine fills the holes.
Most engines share a common vocabulary: variable interpolation (often written with a delimiter such as double curly braces), filters or helpers that transform a value before output, control structures for iteration and conditionals, and template inheritance or partials so a base layout can be extended by individual pages. A critical responsibility is automatic escaping: a good engine escapes interpolated values by default so that user-supplied data cannot inject markup or script, which is a primary defense against cross-site scripting.
Template engines exist across every language and era of web development — ERB and Liquid in Ruby, Blade and Twig in PHP, Jinja and the Django template language in Python, Handlebars, EJS, and Pug in JavaScript, and Thymeleaf and JSP in Java. Client-side frameworks blurred the line by moving rendering into the browser with JSX and reactive templates, but the underlying idea is unchanged: a declarative description of output structure, evaluated against data, to produce the final document.