=== Twig === Contributors: njetskive Donate link: http://martindotcom.se/ Tags: twig, template engine Requires at least: 3.0.1 Tested up to: 4.0 Stable tag: trunk License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Twig lets you use the Twig template engine in your WordPress themes. It's very easy to use but still very powerful. == Description == This WordPress plugin allows you to use the [Twig template engine](http://twig.sensiolabs.org) in your WordPress themes. It is really simple to use and even includes a template wrapper enabling you to use a master layout file to keep your themes D.R.Y. = Usage = Let's start with the most simple use of this plugin to get you going: ` 'post' ]); ` The plugin will now look for the template **post.twig**. = Template folder structure = You can offcourse keep all your templates organized into folders and render them like so: ` Twig::View([ 'template' => 'components/header' ]); Twig::View([ 'template' => 'index' ]); Twig::View([ 'template' => 'components/footer' ]); ` *If you find this an interesting aproach please read about template wrapping below.* = Passing variables to templates = To pass variables to our templates you pass the view function a context parameter. The context parameter is an array of variables of your choice: ` // define variables to pass on to template $name = 'Martin'; $age = 26; $footer_context = [ 'author' => $name, 'date' => date('Y-m-d') ]; Twig::View([ 'template' => 'components/header', // pass on variables to use in template 'context' => [ 'title' => 'Twig' ] ]); Twig::View([ 'template' => 'index', // pass on variables to use in template 'context' => [ 'name' => $name, 'age' => $age ] ]); Twig::View([ 'template' => 'components/header', // pass on variables to use in template 'context' => $footer_context ]); ` The variables we passed on are now available in the templates like so: ` {# index.twig #}

My name is {{ name }} and I am {{ age }} years old!

` This clearly shows how we can use the WordPress template files (ex. **index.php**) to handle all our logic and let the [Twig template engine](http://twig.sensiolabs.org) do what it does best. = Remember Twig = Don't forget that you are using Twig. You can still use the awesome features of Twig such as: multiple inheritance, blocks, automatic output-escaping etc. = Template hierarchy = This plugin utilizes WordPress template hierarchy in a very neat whay. Ex: ` Twig::View( ['template' =>'page-about'] ); ` The plugin will try to find the template **page-about.twig** and gracefully fall back to **page.twig** if it does'nt exist. It will actually fall back like this: `page-about-me.twig -> page-about.twig -> page.twig`. Which will be very handy when we use master layout files. = Template wrapping = The plugin comes with a simple template wrapper. It allows you to specify a master layout file: **_layout.php** and exposes two new functions: ` /** * returns the WordPress template being rendered * * Ex: index, page, page-about */ get_twig_template(); /** * returns the absolute path of the same template * * Ex: ABSPATH/wp-content/themes/active-theme/index.php */ get_twig_template_path(); ` If we've enabled this feature in the admin section we can now create the file **_layout.php** in our theme folder and it might look like this: `