Twig

The flexible, fast, and secure template language for PHP

Library
Composer
vv3.30.0
8,369 stars
BSD 3-Clause License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
89 /100 Excellent
Development Activity 96
Maintenance 72
Community 88
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
81 /100 Excellent
Architecture 87
Code Quality 88
Innovation 72
Learning Curve 75

Twig is a template engine for PHP with a syntax inspired by Django and Jinja, designed to keep view logic clean, secure, and easy to read for both developers and designers. It compiles templates into optimized plain PHP code, providing fast rendering while sandboxing untrusted templates for security-sensitive applications.

Twig is the default templating engine for Symfony and is widely used across the PHP ecosystem, offering a rich expression syntax, template inheritance, extensible filters/functions, and first-class autoescaping to prevent XSS by default.

What You Get

  • A concise, Jinja/Django-inspired template syntax with inheritance, blocks, and macros
  • Automatic output escaping (autoescaping) enabled by default to prevent XSS
  • A sandbox mode for safely rendering templates from untrusted sources
  • An extensible architecture (custom filters, functions, tags, and extensions)
  • Compilation to plain PHP for fast repeated rendering rather than runtime interpretation

Common Use Cases

  • Rendering views in Symfony applications (Twig is Symfony’s default templating engine)
  • Generating HTML emails or PDFs from structured templates in PHP projects
  • Letting non-developers (designers, content editors) safely edit page templates
  • Rendering user-submitted or CMS-managed templates safely via the sandbox extension
  • Building static site generators or code generators on top of a PHP templating layer

Under The Hood

Architecture - Twig parses template source into tokens via Lexer.php, builds an AST with Parser.php/ExpressionParser, and compiles that AST into plain PHP via Compiler.php and the Node/NodeVisitor tree, with the compiled PHP class extending Template.php at runtime and Environment.php acting as the central configuration and extension registry. Tech Stack - Twig is pure PHP (8.1+) with minimal runtime dependencies (Symfony polyfills for mbstring/ctype and deprecation-contracts), and uses PHPStan for static analysis and php-cs-fixer for style enforcement in CI. Code Quality - The tests/ directory contains around a hundred PHP test files covering the lexer, parser, compiler, extensions, sandbox, and loader behavior, run via symfony/phpunit-bridge, and the project maintains a phpstan-baseline.neon to track and reduce static-analysis debt over time. API Design - The Environment/Loader split keeps configuration separate from template resolution, and extension points (custom TwigFilter/TwigFunction/TwigTest registration) let applications extend the language without forking the compiler, though understanding compilation internals (Lexer -> Parser -> Compiler) requires more depth than templates alone for anyone writing custom extensions.

Used by 6 apps in this directory

PHP
78%
Other

Craft CMS

CMS

3,609

A developer-first PHP CMS with clean-slate content modeling, auto-generated GraphQL API, and a four-tier edition system that scales from solo projects to enterprise deployments.

View details
96
Repo Health
83
Technical
62
Dependency
Built with
PHP 78%
JavaScript 14%
Updated 5 days ago
PHP
85%
Other

Invoice Ninja

Invoicing Finance · Project Management

10,123

Self-hostable invoicing, time-tracking, and multi-gateway payment platform for freelancers and small businesses, with built-in e-invoicing compliance for EU and global standards.

View details
97
Repo Health
83
Technical
62
Dependency
Built with
PHP 85%
Updated 1 weeks ago
JavaScript
43%
Other

LimeSurvey

Forms Surveys

3,736

The world's most flexible open-source survey platform with 900+ templates, conditional logic, 80+ languages, and full GDPR compliance for any scale.

View details
86
Repo Health
62
Technical
63
Dependency
Built with
JavaScript 43%
PHP 35%
CSS 12%
Updated 5 days ago
TypeScript
44%
Other

Magic

AI Agents · Automation · Low Code Platforms

5,043

Magic is an enterprise-grade open-source AI agent platform combining a generalist AI agent, workflow engine, IM, and collaborative office system for running an AI-powered digital workforce.

View details
70
Repo Health
79
Technical
65
Dependency
Built with
TypeScript 44%
PHP 32%
Updated 1 months ago
PHP
78%
GPL 3.0

matomo

Analytics

21,905

Open-source, privacy-first web and app analytics that puts you in complete control of your data.

View details
95
Repo Health
82
Technical
62
Dependency
Built with
PHP 78%
Updated 4 days ago
PHP
84%
MIT

wallabag

Bookmarks Archiving

12,989

Self-hosted read-it-later app that saves clean, ad-free articles from any webpage for distraction-free reading across all your devices.

View details
93
Repo Health
81
Technical
59
Dependency
Built with
PHP 84%
Twig 13%
Updated 4 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers