{"id":2508,"date":"2025-03-05T11:40:45","date_gmt":"2025-03-05T10:40:45","guid":{"rendered":"https:\/\/www.speich.net\/articles\/?p=2508"},"modified":"2025-05-04T16:45:33","modified_gmt":"2025-05-04T14:45:33","slug":"twig-tip-ternary-operator-with-an-undefined-variable","status":"publish","type":"post","link":"https:\/\/www.speich.net\/articles\/en\/2025\/03\/05\/twig-tip-ternary-operator-with-an-undefined-variable\/","title":{"rendered":"Twig tip: Ternary operator with an undefined variable"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You can use the <code>default<\/code> filter in combination with the ternary operator <code>:?<\/code> on a variable within a Slim Twig template when the variable is undefined:<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>{{ myVar|default ? '' : ' class=\"myClass\"'}}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or in an if statement:<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>{% if myVar|default %}\n  ...\n{% endif %}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the variable is undefined and should not be an empty string:<\/p>\n\n\n\n<pre class=\"wp-block-code language-php\"><code>{{ myVar|default('') == '' ? '' : ' class=\"myClass\"'}}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can use the default filter in combination with the ternary operator \ud83d\ude15 on a variable within a Slim Twig template when the variable is undefined: or in an if statement: When the variable is undefined and should not be an empty string:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,102],"tags":[357,361],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-php","category-uncategorized-en","tag-slim-framework","tag-twig-template-engine","entry"],"_links":{"self":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/comments?post=2508"}],"version-history":[{"count":6,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2634,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2634"}],"wp:attachment":[{"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.speich.net\/articles\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}