

Make sure to include a cache directory, or Twig can’t cache the compiled templates.There are a couple of options to consider here. $twigEnvironment -> addExtension ( new \Twig \Extension \DebugExtension ( ) ) $twigEnvironment = new \Twig \Environment ( $twigLoader = new \Twig \Loader \FilesystemLoader ( $config -> twigDirectory ) $config -> twigDirectory = $config -> paths -> root.

Now, initialize the FileSystemLoader in the prepend template file _init.php. The directory structure will look like this. I already have a src directory for PHP code, so I’ll add my Twig template folder right next to it. Since the Twig templates shouldn’t be publicly accessible, we can put them outside the webroot. Twig needs two things to run: A FileSystemLoader with the path to your templates and an Environment to render them. $config -> appendTemplateFile = '_main.php' Step 1: Set up the Twig environment # Make sure to set your prepend and append file in your config.php: $config -> prependTemplateFile = '_init.php'
#IDE FOR PROCESSWIRE HOW TO#
Installation, file and request structure #įirst, install Twig with Composer (see How to set up and integrate Composer with ProcessWire): composer require "twig/twig:^2.0" I have written a dedicated explanation why I prefer Twig to PHP. If you are unfamiliar with Twig, read the Twig guide for Template Designers and Twig for Developers and then come back to this tutorial. However, I will not include a general introduction to the Twig language.
#IDE FOR PROCESSWIRE CODE#
Note that there is a Twig module that includes Twig automatically, but integrating it manually requires only a couple lines of code and allows you to set up your environment and template structure exactly how you want it. This tutorial will show you how to create your own flexible, extendable template system with Twig and integrate it seamlessly into a ProcessWire site. You also get some neat features for template structuring out of the box, especially template inheritance which will help you keep your code DRY. Using a dedicated template language helps you adhere to this separation of concerns, because it doesn’t allow you to do all the heavy processing you can do in pure PHP. However, as a project grows in complexity, it may benefit from separating the logic (input processing, data preparation etc.) from the view (HTML output through templates). ProcessWire sites can run on simple PHP without any additional templating language (after all, PHP started out as a template language itself) - and it’s totally fine to leave it that way. How to set up Twig as a flexible view layer for ProcessWire Create a base template for content sections.Create an HTML skeleton in the default page template.Step 3: Render the appropriate Twig template.Step 2: Perform logic and pass data in PHP templates.Installation, file and request structure.Toggle site navigation Skip to main content Overview
