Twig is a template engine for PHP. It parses templates into tags, prints, and blocks. The lexer tokenizes templates into tokens, and the parser converts tokens into an AST. The compiler generates PHP code from the AST. Twig supports tags like for loops and if/else, macros, and internationalization. It can be extended with custom tags, filters, functions. Security features include escaping, sandboxing and permissions. Templates can access objects and be rendered without controllers.