{"id":30707,"date":"2014-07-28T21:05:58","date_gmt":"2014-07-28T21:05:58","guid":{"rendered":"https:\/\/wordpress.org\/plugins-wp\/twig\/"},"modified":"2014-10-13T09:18:09","modified_gmt":"2014-10-13T09:18:09","slug":"twig","status":"closed","type":"plugin","link":"https:\/\/wordpress.org\/plugins\/twig\/","author":13949593,"comment_status":"closed","ping_status":"closed","template":"","meta":{"version":"1.0.2","stable_tag":"trunk","tested":"4.0.38","requires":"3.0.1","requires_php":"","requires_plugins":"","header_name":"Twig","header_author":"Martin Pettersson","header_description":"","assets_banners_color":"","last_updated":"2014-10-13 09:18:09","external_support_url":"","external_repository_url":"","donate_link":"http:\/\/martindotcom.se\/","header_plugin_uri":"http:\/\/github.com\/martin-pettersson\/WP_Twig","header_author_uri":"","rating":0,"author_block_rating":0,"active_installs":10,"downloads":884,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation"],"tags":[],"upgrade_notice":[],"ratings":{"1":0,"2":0,"3":0,"4":0,"5":0},"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":[],"block_files":[],"assets_screenshots":[],"screenshots":[]},"plugin_section":[],"plugin_tags":[19995,17808],"plugin_category":[],"plugin_contributors":[96295],"plugin_business_model":[],"class_list":["post-30707","plugin","type-plugin","status-closed","hentry","plugin_tags-template-engine","plugin_tags-twig","plugin_contributors-njetskive","plugin_committers-njetskive"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/twig.svg","icon_2x":false,"generated":true},"screenshots":[],"raw_content":"<!--section=description-->\n<p>This WordPress plugin allows you to use the <a href=\"http:\/\/twig.sensiolabs.org\">Twig template engine<\/a> 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.<\/p>\n\n<h4>Usage<\/h4>\n\n<p>Let's start with the most simple use of this plugin to get you going:<\/p>\n\n<pre><code>&lt;?php \/\/ index.php\n\nTwig::View();\n<\/code><\/pre>\n\n<p>And that's it! The plugin will automatically know that you are in the <strong>index.php<\/strong> file, and try to find the template <strong>index.twig<\/strong> in any of the specified template folders.\nWe will later see how we can use this principle in a very creative manner.<\/p>\n\n<p>Of course we can specify wich template to use, we do this without the file extension like so:<\/p>\n\n<pre><code>Twig::View([\n    'template' =&gt; 'post'\n]);\n<\/code><\/pre>\n\n<p>The plugin will now look for the template <strong>post.twig<\/strong>.<\/p>\n\n<h4>Template folder structure<\/h4>\n\n<p>You can offcourse keep all your templates organized into folders and render them like so:<\/p>\n\n<pre><code>Twig::View([\n    'template' =&gt; 'components\/header'\n]);\n\nTwig::View([\n    'template' =&gt; 'index'\n]);\n\nTwig::View([\n    'template' =&gt; 'components\/footer'\n]);\n<\/code><\/pre>\n\n<p><em>If you find this an interesting aproach please read about template wrapping below.<\/em><\/p>\n\n<h4>Passing variables to templates<\/h4>\n\n<p>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:<\/p>\n\n<pre><code>\/\/ define variables to pass on to template\n$name = 'Martin';\n$age = 26;\n\n$footer_context = [\n    'author' =&gt; $name,\n    'date' =&gt; date('Y-m-d')\n];\n\nTwig::View([\n\n    'template' =&gt; 'components\/header',\n\n    \/\/ pass on variables to use in template\n    'context' =&gt; [\n        'title' =&gt; 'Twig'\n    ]\n]);\n\nTwig::View([\n\n    'template' =&gt; 'index',\n\n    \/\/ pass on variables to use in template\n    'context' =&gt; [\n        'name' =&gt; $name,\n        'age' =&gt; $age\n    ]\n]);\n\nTwig::View([\n\n    'template' =&gt; 'components\/header',\n\n    \/\/ pass on variables to use in template\n    'context' =&gt; $footer_context\n]);\n<\/code><\/pre>\n\n<p>The variables we passed on are now available in the templates like so:<\/p>\n\n<pre><code>{# index.twig #}\n&lt;p&gt;My name is {{ name }} and I am {{\u00a0age }}\u00a0years old!&lt;\/p&gt;\n<\/code><\/pre>\n\n<p>This clearly shows how we can use the WordPress template files (ex. <strong>index.php<\/strong>) to handle all our logic and let the <a href=\"http:\/\/twig.sensiolabs.org\">Twig template engine<\/a> do what it does best.<\/p>\n\n<h4>Remember Twig<\/h4>\n\n<p>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.<\/p>\n\n<h4>Template hierarchy<\/h4>\n\n<p>This plugin utilizes WordPress template hierarchy in a very neat whay. Ex:\n     Twig::View( ['template' =&gt;'page-about'] ); \nThe plugin will try to find the template <strong>page-about.twig<\/strong> and gracefully fall back to <strong>page.twig<\/strong> if it does'nt exist. It will actually fall back like this:\n    page-about-me.twig -&gt; page-about.twig -&gt; page.twig. Which will be very handy when we use master layout files.<\/p>\n\n<h4>Template wrapping<\/h4>\n\n<p>The plugin comes with a simple template wrapper. It allows you to specify a master layout file: <strong>_layout.php<\/strong> and exposes two new functions:<\/p>\n\n<pre><code>\/**\n * returns the WordPress template being rendered\n * \n * Ex: index, page, page-about\n *\/\nget_twig_template();\n\n\/**\n * returns the absolute path of the same template\n *\n * Ex: ABSPATH\/wp-content\/themes\/active-theme\/index.php\n *\/\nget_twig_template_path();\n<\/code><\/pre>\n\n<p>If we've enabled this feature in the admin section we can now create the file <strong>_layout.php<\/strong> in our theme folder and it might look like this:<\/p>\n\n<pre><code>&lt;?php \/\/ _layout.php\n\nget_header( get_twig_template() );\n\ninclude get_twig_template_path();\n\nget_footer( get_twig_template() );\n<\/code><\/pre>\n\n<p>WordPress will now attempt to include <strong>header.php<\/strong>, the WordPress template (ex. <strong>index.php<\/strong>) and the <strong>footer.php<\/strong>.\nNotice that doing this allows us to make <strong>the get_header()<\/strong> and <strong>get_footer()<\/strong> calls only once. We can now focus on the contents of our WordPress template files, and the header and footer files will be automatically included.\nBecause of the two new functions, if we have a file called <strong>header-page.php<\/strong> in our theme. It will be included when we visit a WordPress 'page'.<\/p>\n\n<h4>Template wrapper hierarchy<\/h4>\n\n<p>The (second) best thing about the template wrapper is that it also follows the template hierarchy. Thus we can create the file: <strong>_layout-page.php<\/strong> and it will we used when visiting a WordPress 'page'. We can even create the file: <strong>_layout-page-about.php<\/strong> and it will be used on our about page.<\/p>\n\n<!--section=installation-->\n<ol>\n<li>Upload the entire plugin folder to your WordPress installation's plugin directory<\/li>\n<li>Activate the plugin through the 'Plugins' menu in WordPress<\/li>\n<li>Specify a Twig installation path and a folder for your templates in the admin section (settings\/twig)<\/li>\n<\/ol>","raw_excerpt":"Twig lets you use the Twig template engine in your WordPress themes. It&#039;s very easy to use but still very powerful.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/30707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=30707"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/njetskive"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=30707"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=30707"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=30707"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=30707"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=30707"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=30707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}