twig

41
Twig & Drupal 8

Upload: sir-arturio

Post on 11-May-2015

785 views

Category:

Technology


0 download

DESCRIPTION

Twig presentation for DrupalCamp Finland 2013

TRANSCRIPT

Page 1: Twig

Twig &

Drupal 8

Page 2: Twig

Joonas Pajunen

• Fraktio Oy

• Symfony2 Developer

• Twitter: joonsp

Page 3: Twig

Arto Iijalainen

• Druid Oy

• Drupal backend developer

• Drupal.org: Sir-Arturio

• Twitter: arto_iijalainen

Page 4: Twig

Twig

Page 5: Twig

Overview

• Twig in general

• Why Twig is in Drupal 8?

• Twig’s solutions to Drupal’s problems

• Current state of the project & conclusion

Page 6: Twig

Templating engine

• Used in Symfony and other projects

• Twig is not a Symfony component

• Inspired by Django, originally by Armin Ronacher, via and maintained by Fabien Potencier

Page 7: Twig

<body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul>

<h1>My Webpage</h1> {{ a_variable }}

{# <p> unimplemented feature </p> #}</body>

Syntax

Page 8: Twig

<body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul>

<h1>My Webpage</h1> {{ a_variable }}

{# <p> unimplemented feature </p> #}</body>

• print {{ }}

Syntax

Page 9: Twig

<body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul>

<h1>My Webpage</h1> {{ a_variable }}

{# <p> unimplemented feature </p> #}</body>

• print {{ }}

• execute statements {% %}

Syntax

Page 10: Twig

<body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul>

<h1>My Webpage</h1> {{ a_variable }}

{# <p> unimplemented feature </p> #}</body>

• print {{ }}

• execute statements {% %}

• comment {# #}

Syntax

Page 11: Twig

Language features

• control structures

• tests

• variables

• logic

• math

Page 12: Twig

<ul>{% for user in users %}

{% if loop.index is divisibleby(2) and loop.length < 2 %} <li class=”even”> {% else%} <li class=”odd”> {% endif %}

{{ user.username }} </li>{% else %} <li>no users</li>{% endfor %}</ul>

Page 13: Twig

Variable access

• array key

• object property

• getBar or isBar function

{{ foo.bar }}

Page 14: Twig

Composability

• extends

• include

• use

• block

Page 15: Twig

{# base.html.twig #}

<head> {% block head %} {% endblock %}</head><body> <div id="content">

{% block content %}{% endblock %} </div></div>

{# page.html.twig #}

{% extends "base.html.twig" %}{% block content %} <h1>Index</h1> <p class="important"> Welcome to my awesome homepage. </p>{% include "footer.html.twig" %}{% endblock %}

Page 16: Twig

Filters, functions

{{ post.published_at|date("m/d/Y") }}

{{ [“seppo”, “kimmo”]|first|upper }} -> SEPPO

{{ random(['apple', 'orange', 'citrus']) }}

Page 17: Twig

Extensibility

• custom functions, tags, filters

• can override and customize syntax

Page 18: Twig

class MoneyExtension extends Twig_Extension{    public function getFunctions() {        return array(            'cents_to_euros' => new Twig_Function_Method($this, 'centsToEuros')        );    }

    public function centsToEuros($cents) {             return round($cents/100, 2);    }

    public function getName() {        return 'money';    }}

Example: creating a custom function

Page 19: Twig

$twig = new Twig_Environment($loader);$twig->addExtension(new MoneyExtension());

{{ money(250) }} €

becomes:

2.5 €

Example: creating a custom function (usage)

Page 20: Twig

Speed

• parsed -> optimized php -> cached

• C-extension available

Page 21: Twig

Documentation

• Extensive documentation available!

• http://twig.sensiolabs.org/documentation

Page 22: Twig

Good IDE highlight support

• phpstorm

• netbeans

• eclipse

• sublime text

• notepad++

• vim

• textmate

• etc …

Page 23: Twig

WhyD8

+ ?

Page 24: Twig

New theme layer!D8

Page 25: Twig

Why do we need a new theme layer?

What’s wrong with D7’s theme layer?or

Page 27: Twig

Flaws in D7’s theme layer

• PHPtemplate is insecure

• Template language is Drupal-only (PHPtemplate + drupalisms)

• Lots of different ways to address variables

• Theme system is overly complex

• Two ways of creating markup: templates and theme functions

• Too many and too cluttered templates

Page 28: Twig

PHPtemplate is insecure<?php db_query("DROP TABLE {node}"); unlink('sites/default/files/myfilename.pdf'); ?>

Page 29: Twig
Page 30: Twig

PHPtemplate is insecure<?php db_query("DROP TABLE {node}"); unlink('sites/default/files/myfilename.pdf'); ?>

Not possible in Twig! \o/

Page 31: Twig

Twig - Security

• autoescape by default

• sandbox

Page 32: Twig

Template language is Drupal-only

Twig is proudly found elsewhere!No need to reinvent the wheel.

Page 33: Twig

Lots of different ways to address variables

• array key

• object property

• getBar or isBar function

{{ foo.bar }}

• $foo[‘bar’]

• $foo->bar

• $foo->!etBar()$foo->isBar()

PHPtemplate Twig

Page 34: Twig

Theme system is overly complex

http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8

Page 35: Twig

Theme system is overly complex

http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8

Page 36: Twig

Two ways of creating markup

theme_admin_view();admin-view.tpl.php

Only Twig templates left!admin-view.html.twig

Page 37: Twig

Too many and too cluttered templates

Templates will be cleaned and consolidated in the refactoring process.

Page 38: Twig

Flaws in D7’s theme layer

• PHPtemplate is insecure

• Template language is Drupal-only (PHPtemplate + drupalisms)

• Lots of different ways to address variables

• Theme system is overly complex

• Two ways of creating markup: templates and theme functions

• Too many and too cluttered templates

}}

Twig will fix

Theme layer will fix

Page 39: Twig

Current development status• *DONE* Twig is now an official template engine in D8 core

• *IN PROGRESS* Templates are being converted on Twig

• *WAITING* Twig as a main template engine

• *IN PROGRESS* Markup refactoring

• … Creating Dream Markup

• … Rely on Twig auto-escape

• … Finish template suggenstions

• HELP NEEDED!

Page 40: Twig

Conclusions …

Page 41: Twig

Thanks!