talira

The flexible, fast, and secure template engine for PHP

open sourceBSD-3-Clausefree

Twig is a template engine for PHP. It compiles templates down to plain optimized PHP, escapes output with a short syntax ({{ var|e }}), supports multiple inheritance and blocks, and has a sandbox mode for evaluating template code users can edit. Its lexer and parser let developers define their own tags and filters, and even a DSL. Fabien Potencier leads it; Armin Ronacher founded the project.

Problems it solves

  • my PHP views are full of echo and htmlspecialchars calls
  • let customers edit email or page templates without running arbitrary PHP
  • a template language for PHP with layouts and inheritance

Features

  • a template engine for PHP with concise syntax and automatic output escaping
  • compiles templates to optimized PHP
  • template inheritance, blocks and macros
  • sandbox mode for templates users can edit
  • custom tags, filters and functions

Limitations

  • Twig 3 needs PHP 8.1 or later.1

Alternatives

The same job

These do what Twig does.

  1. Latte PHP template engine with context-sensitive escaping
  2. Smarty (PHP) The compiling template engine for PHP
  3. Jinja A very fast and expressive template engine

Compare Twig with the top 3