{"id":573,"date":"2014-09-10T12:25:28","date_gmt":"2014-09-10T19:25:28","guid":{"rendered":"http:\/\/www.keganv.com\/?p=573"},"modified":"2018-05-18T14:18:21","modified_gmt":"2018-05-18T21:18:21","slug":"being-cool-with-twig-templating","status":"publish","type":"post","link":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/","title":{"rendered":"Being Cool With Twig Templating"},"content":{"rendered":"<p>So this is going to be an ongoing post, (when I remember to update it), about cool things in the Twig templating engine that you can do, but may not have known about. If you have some cool shortcuts or tricks, definitely leave them in the comments and I&#8217;ll add them to the list!<\/p>\n<h3>Simple Blocks<\/h3>\n<p>For simple blocks, like a &#8216;title&#8217; block that may only contain a string you can turn this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% block title %}\r\n    My Title Goes Here\r\n{% endblock title %}\r\n<\/pre>\n<p>into this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% block title 'My Title Goes Here' %}\r\n<\/pre>\n<p><!--more--><\/p>\n<h3>Set Variables<\/h3>\n<p>Setting variables within twig is super easy. In the example below I&#8217;m setting the variable as a list of names. You probably wouldn&#8217;t do it like this in a real scenario, but you get the gist.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% set names = &#x5B;'Tiffany', 'Rider', 'Kade', 'Sean', 'Nick', 'Mike'] %}\r\n<\/pre>\n<h3>Loop Variable<\/h3>\n<p>The <code>loop<\/code> variable can come in especially handy and is available in a <code>for<\/code> tag. Let&#8217;s say we wanted to loop our list of names and do something special on the first and last iteration. We could use the loop.first and loop.last variables.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% for name in names %}\r\n    {% if loop.first %}\r\n        \/\/ Do something on first iteration\r\n    {% endif %}\r\n    {% if loop.last %}\r\n        \/\/ Do something on last iteration\r\n    {% endif %}\r\n{% endfor %}\r\n<\/pre>\n<p>There are plenty more where that came from. Check out more loop variables in <a href=\"http:\/\/twig.sensiolabs.org\/doc\/tags\/for.html#the-loop-variable\" target=\"_blank\">the official docs<\/a>.<\/p>\n<h3>Condition In Your For Loop<\/h3>\n<p>Another cool feature you can take advantage of in a loop is a condition which will filter what gets output. Let&#8217;s look at the names array again as an example.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;ul&gt;\r\n    {% for name in names if name|length &gt; 5 %}\r\n        &lt;li&gt;{{ name }}&lt;\/li&gt;\r\n    {% endfor %}\r\n&lt;\/ul&gt;\r\n<\/pre>\n<p>The above code will only display &#8220;Tiffany&#8221; on output.<\/p>\n<h3>Printing Out Blocks Multiple Times<\/h3>\n<p>If you are using a template that inherits (extends) another template, you can print out a block of content multiple times by just using the block function.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% block user %}{% endblock user %}\r\n&lt;div&gt;\r\n    {{ block('user') }}\r\n&lt;\/div&gt;\r\n<\/pre>\n<h3>Removing Whitespace<\/h3>\n<p>Their are numerous ways to remove white space from the output. The first and maybe the most commonly seen is the <code>spaceless<\/code> tag.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% spaceless %}\r\n    &lt;div&gt;\r\n        &lt;p&gt;Here is some text.&lt;\/p&gt;\r\n    &lt;\/div&gt;\r\n{% endspaceless %}\r\nrenders: &lt;div&gt;&lt;p&gt;Here is some text.&lt;\/p&gt;&lt;\/div&gt;\r\n<\/pre>\n<p>Another, and maybe less known way to remove whitespace from output is using a &#8220;-&#8221; on an individual tag. This allows you to trim leading or trailing whitespace.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% set var = 'Here is some text.' %}\r\n{%- if true -%}\r\n    &lt;p&gt;\r\n        {{- var -}}\r\n    &lt;\/p&gt;\r\n{%- endif -%}\r\nrenders: &lt;p&gt;Here is some text.&lt;\/p&gt;\r\n<\/pre>\n<p>You can also just trim white space on one side or the other if necessary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% set var = 'Here is some text.' %}\r\n&lt;p&gt;    {{ var -}}    &lt;\/p&gt;\r\nrenders: &lt;p&gt;    Here is some text.&lt;\/p&gt;\r\n<\/pre>\n<h3>Twig Tests<\/h3>\n<p>Twig is pretty awesome at logic tests. For example if you want to check if one variable is <em>exactly<\/em> equal to another variable, (===), you can use the <code>is same as(variable)<\/code> test.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n{% if entityType is same as('Account') %}\r\n    The entity is an Account!\r\n{% endif %}\r\n<\/pre>\n<h3>Display Class Constants<\/h3>\n<p>Displaying a class&#8217;s constant value in a Twig file is simply achieved with the <code>constant()<\/code> function. Let&#8217;s say that the <code>RECORDS_TO_SHOW<\/code> constant has a value of 10.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;p&gt;There are {{ constant(RECORDS_TO_SHOW, entity) }} per page.&lt;\/p&gt;\r\nrenders: &lt;p&gt;There are 10 records per page.&lt;\/p&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"So this is going to be an ongoing post, (when I remember to update it), about cool things in the Twig templating engine that you can do, but may not have known about. If you have some cool shortcuts or tricks, definitely leave them in the comments and I&#8217;ll add them to the list! Simple &#8230; <br\/> <a class=\"view-article btn btn-primary flip pull-left\" href=\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\"><span>View Article<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-573","post","type-post","status-publish","format-standard","hentry","category-coding-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Twig Tricks - Being Cool With Twig Templating | KeganV.com<\/title>\n<meta name=\"description\" content=\"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Twig Tricks - Being Cool With Twig Templating | KeganV.com\" \/>\n<meta property=\"og:description\" content=\"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\" \/>\n<meta property=\"og:site_name\" content=\"KeganV\" \/>\n<meta property=\"article:published_time\" content=\"2014-09-10T19:25:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-05-18T21:18:21+00:00\" \/>\n<meta name=\"author\" content=\"Kegan V.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kegan V.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\"},\"author\":{\"name\":\"Kegan V.\",\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"headline\":\"Being Cool With Twig Templating\",\"datePublished\":\"2014-09-10T19:25:28+00:00\",\"dateModified\":\"2018-05-18T21:18:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\"},\"wordCount\":615,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"articleSection\":[\"Coding &amp; Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\",\"url\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\",\"name\":\"Twig Tricks - Being Cool With Twig Templating | KeganV.com\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/#website\"},\"datePublished\":\"2014-09-10T19:25:28+00:00\",\"dateModified\":\"2018-05-18T21:18:21+00:00\",\"description\":\"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.keganv.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Being Cool With Twig Templating\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.keganv.com\/#website\",\"url\":\"https:\/\/www.keganv.com\/\",\"name\":\"KeganV\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.keganv.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\",\"name\":\"Kegan V.\",\"logo\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.keganv.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Twig Tricks - Being Cool With Twig Templating | KeganV.com","description":"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/","og_locale":"en_US","og_type":"article","og_title":"Twig Tricks - Being Cool With Twig Templating | KeganV.com","og_description":"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!","og_url":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/","og_site_name":"KeganV","article_published_time":"2014-09-10T19:25:28+00:00","article_modified_time":"2018-05-18T21:18:21+00:00","author":"Kegan V.","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kegan V.","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#article","isPartOf":{"@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/"},"author":{"name":"Kegan V.","@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"headline":"Being Cool With Twig Templating","datePublished":"2014-09-10T19:25:28+00:00","dateModified":"2018-05-18T21:18:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/"},"wordCount":615,"commentCount":0,"publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"articleSection":["Coding &amp; Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/","url":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/","name":"Twig Tricks - Being Cool With Twig Templating | KeganV.com","isPartOf":{"@id":"https:\/\/www.keganv.com\/#website"},"datePublished":"2014-09-10T19:25:28+00:00","dateModified":"2018-05-18T21:18:21+00:00","description":"Shortcuts or tricks you may not have known with the Twig templating engine. Leave any Twig tricks you know about in the comments!","breadcrumb":{"@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.keganv.com\/being-cool-with-twig-templating\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.keganv.com\/being-cool-with-twig-templating\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.keganv.com\/"},{"@type":"ListItem","position":2,"name":"Being Cool With Twig Templating"}]},{"@type":"WebSite","@id":"https:\/\/www.keganv.com\/#website","url":"https:\/\/www.keganv.com\/","name":"KeganV","description":"","publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.keganv.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276","name":"Kegan V.","logo":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.keganv.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/573"}],"collection":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/comments?post=573"}],"version-history":[{"count":34,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":1208,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/573\/revisions\/1208"}],"wp:attachment":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}