On Github lauriii / twig-slides
<header role="banner"> <h1>Dries FTW!</h1> <nav> <a href="/">home</a> | <a href="/info">info</a> </nav> </header> <main> <aside> ... </aside> <div> ... </div> </main> <footer role=""> </footer>
<body class="html drupal7 is-awesome wtf not-logged-in no-sidebars page-node page-node- page-node-274 node-type-page">
<body>
.field { margin: 6px 9px; } .field.field--name { color: pink; }
<div class="field field--name" />
<?php db_query(' DROP DATABASE; ROLLBACK; '); $frontend_developers = 'rule';
<?php // My test variable print $variable;
{# My test variable #} {{ variable }}
{% set variable = 'result' %}
{% set array = [ 'foo', 'bar', ] %}
<?php // WTF?! print $hamburger['sandwich']['und']->content['meat']['bacon']['stuff'] ?>
{{ hamburger.sandwich.content.meat.bacon.stuff }}
{{ dump() }}
{{ dump(foo) }}
<h2>Team Awesome</h2> <ul> {% for user in users %} <li>{{ user.username}}</li> {% endfor %} </ul>
<h2>Team Awesome</h2> <ul> <li>cottser</li> <li>joel</li> <li>david</li> <li>mark carver</li> <li>mortendk</li> </ul>
{{ loop.length }} {{ loop.first }} {{ loop.last }} {{ loop.index }} {% if loop.first %} ... {% elseif loop.index == 2 %} ... {% elseif loop.last %} ... {% endif %}
<h2>Team Awesome</h2> <ul> {% for user in users %} <li>{{ user.username}}</li> {% else %} <li>no results found</li> {% endfor %} </ul>
<p> {% filter upper %} uppercase for the win {% endfilter %} </p>
{% set name = 'Lauri' %} <span> {{ name|length }} </span>
<span> 5 </span>
{% block headerblock %} <h2>This block needs more kittens!</h2> {% endblock %}page--front.html.twig
{% extends "page.html.twig" %} {% block headerblock %} {{ parent() }} <h4>Kitten!</h4> {% endblock %}
# engine: phptemplatethemename.info
{% set class_name = 'lauri/druid' %} {% set contributors = [ 'rteijeiro', 'joelpittet', 'Cottser', 'lauriii', 'Dries', ] %} {{ class_name|clean_class }} {{ contributors|without('lauriii') }}, {{ stuff|placeholder('kitten') }}
lauri-druid rteijeiro,joelpittet,Cottser,Dries, kitten
<?php function template_preprocess_username(&$variables) { $variables['attributes'] = new Attribute(); } ?>
<div{{ attributes.setAttribute('id', 'kitten').setAttribute('I-Love', 'Dries') }}> <div{{ attributes.removeAttribute('id') }}>
<div{{ attributes.addClass('field-item-' ~ name|clean_class) }}>
<?php function node_preprocess_node(&$variables) { $variables['attributes']->addClass(['carrot', 'potato']); }
<div{{ attributes.removeClass('carrot', 'potato').addClass('bacon', 'beef') }}>
{{ 'Author: @username'| t({'@username':username}) }}
{% trans %} Author {{ username }} {% endtrans %}
<!-- THEME DEBUG --> <!-- CALL: _theme('page') --> <!-- FILE NAME SUGGESTIONS: * page--front.html.twig * page--node.html.twig x page.html.twig --> <!-- BEGIN OUTPUT from 'core/themes/bartik/templates/page.html.twig' -->
Basic idea: everytime variable goes to template run appropriate escaping function!
<?php Drupal\Component\Utility\SafeMarkup::isSafe(); Drupal\Component\Utility\SafeMarkup::escape(); Drupal\Component\Utility\SafeMarkup::checkAdminXss();
<?php public static function escape($string) { return static::isSafe($string) ? $string : String::checkPlain($string); }
{% set classes = [ 'node', 'node--type-' ~ node.bundle|clean_class, node.isPromoted() ? 'node--promoted', node.isSticky() ? 'node--sticky', not node.isPublished() ? 'node--unpublished', view_mode ? 'node--view-mode-' ~ view_mode|clean_class, 'clearfix', ] %} <article{{ attributes.addClass(classes) }}> {{ content }} </article>