On Github lauriii / twig-minicamp-turku-14
<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
// My test variable
print $variable;
{# My test variable #}
{{ variable }}
{% set variable = 'result' %}
{%
set array = [
'foo',
'bar',
]
%}
<?php
// WTF?!
print $foo['im']['und']->boss['long']['ass']['shit'] ?>
{{ foo.im.boss.long.ass.mofo }}
<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>
<h2>Team Awesome</h2>
<ul>
{% for user in users %}
<li>{{ user.username}}</li>
{% else %}
<li>no results found</li>
{% endfor %}
</ul>
{{ loop.length }}
{{ loop.first }}
{{ loop.last }}
{{ loop.index }}
{% if loop.first %}
...
{% elseif loop.index == 2 %}
...
{% elseif loop.last %}
...
{% endif %}
<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: phptemplate
themename.info
{% set class_name = 'lauri/druid' %}
{%
set contributors = [
'rteijeiro',
'joelpittet',
'Cottser',
'lauriii',
'Dries',
]
%}
{{ class_name|clean_class }}
{{ contributors|without('lauriii') }},
lauri-druid
rteijeiro,joelpittet,Cottser,Dries,
<?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) }}>
OR
<div{{ attributes.removeClass('GTFO', 'i-hate--you') }}>
{{ 'Author: @username'| t({'@username':username}) }}
{% trans %}
Author {{ username }}
{% endtrans %}
debug: true
It works and it's awesome!
<!-- 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>