-
Twig Overview
Twig is the preferred view rendering system in Vanilla's PHP codebase since version 3.0. It offers a few notable improvements over Smarty or creating views directly in PHP. Automatic escaping/sanitization of content (more secure). More efficient rendering (faster). Better developer documentation (more community support).
-
How To Render a Twig View
There are multiple contexts in Vanilla where you may want to render a twig view. In a Gdn_Controller This is very easy. Follow the rules for naming views as defined in our views documentation. Then use the file extension .twig instead of .php . View overrides, lookup, etcetera, all work exactly the same as with a PHP or…
-
Twig Tips & Tricks
Here are a few simple tips to get the most out of Twig views. Handling null or undefined values In development, Twig will give fatal errors when accessing an undefined value. This is to make the developer more aware of the content being injected into their view, and to more easily identify typos. In production these will…
-
Using a Twig Master View
For most of Vanilla's existence, themes have been able to specify a "master view" or template for a site, generally located at my-theme/views/default.master.tpl. This was the full template that the page would get rendered in. Today, Vanilla offers a newer, twig templating system for creating theme layouts. Rather than…
-
Common Twig Functions
Built In Twig Functions Twig comes with some built-in functions and filters that can be useful when creating view templates. The most common include: Default Values The |default and |default("Default Value") filters apply some default value when used with a null value. A common use case is safely accessing some variable…