explaingit

twigphp/twig

8,358PHPAudience · developerComplexity · 2/5Setup · easy

TLDR

A fast, secure template language for PHP that keeps HTML pages separate from your PHP code, compiles templates to cached PHP classes, and sandboxes untrusted templates so users can't run arbitrary code.

Mindmap

mindmap
  root((Twig))
    What it does
      PHP templating
      HTML separation
      Template inheritance
    Key features
      Compiled and cached
      Sandbox security
      Custom tags
    Syntax
      Double curly braces
      Block inheritance
      Inline filters
    Used in
      Symfony framework
      Any PHP project
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Keep HTML layout files separate from PHP business logic in any PHP project or Symfony app.

USE CASE 2

Safely render user-submitted templates without letting them execute arbitrary PHP code, using Twig's sandbox mode.

USE CASE 3

Define a reusable base page layout with header and footer, then override only the content block in each page template.

USE CASE 4

Transform and format data inline in templates using Twig filters, without touching PHP controller code.

Tech stack

PHP

Getting it running

Difficulty · easy Time to first run · 30min

In plain English

Twig is a template language for PHP. PHP is a programming language commonly used to build websites, and Twig lets developers keep the HTML structure of their pages separate from the PHP code that generates the data. Instead of mixing PHP logic directly into HTML files, you write templates in Twig's own syntax, which is cleaner and easier to read. The syntax was inspired by Django and Jinja, which are template languages used in the Python world. Developers familiar with those will find Twig's style familiar. It uses double curly braces to output variables, blocks for defining sections that child templates can override, and filters for transforming data inline. Twig is most commonly encountered as the default templating layer in Symfony, a widely used PHP web framework, but it can be used independently in any PHP project. It is designed with three goals: flexibility (it can be extended with custom tags and filters), performance (templates are compiled to PHP classes and cached), and security (it sandboxes untrusted template code so that user-supplied templates cannot execute arbitrary PHP). The README for this repository is minimal and points readers to the official documentation website for setup and usage details. The project is mature and widely adopted in the PHP ecosystem, with a long maintenance history.

Copy-paste prompts

Prompt 1
Show me how to set up the Twig environment in a plain PHP project, load templates from a folder, enable caching, and render a template that displays a list of items passed as a variable.
Prompt 2
Write a Twig base template with named blocks for header, content, and footer, then show a child template that extends it and fills only the content block.
Prompt 3
I need to let users paste their own Twig templates in my PHP app. Show me how to enable Twig's sandbox mode with a security policy that allows only specific safe tags and filters.
Prompt 4
How do I create a custom Twig filter in PHP that takes a number and returns it formatted as a dollar currency string, then register it on the Twig environment?
Open on GitHub → Explain another repo

← twigphp on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.