Top Banner
CROWD Communications Group, LLC Drupal 8 eming Exploring Twig & Other Frontend Changes
35

Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Aug 07, 2015

Download

Technology

Sean T. Walsh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

CROWDCommunications Group, LLC

CROWDCommunications Group, LLC

Drupal 8 ThemingExploring Twig & Other Frontend Changes

Page 2: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

About Me

Sean T. [email protected]

@seantwalsh@crowdcg

irc: crowdcg

Page 3: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Agenda• What is Twig & Why is it in D8

• Improving the Themer Experience

• Getting Involved

• Questions

Page 4: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Twig & D8

Page 5: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

A New Template Engine Drupal 7 = PHPTemplate

• Conflict between Back-end & Front-end

• Potential Security Issues

• 55 templates154 functions

Drupal 8 = Twig

• Keeps Back-end & Front-end Separated

• More Secure - Autoescaping

• 149 templates21 functions

Page 6: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Crash Course

Page 7: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Comments & VarsPHP Twig

<?php

// My test variable print $variable;

{# My test variable #}{{ variable }}

Page 8: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Set Variables

{% set variable = 'result' %}

{% set array = [ 'foo', 'bar', ]%}

Page 9: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

ArraysPHP

Twig

<?php

print $foo[‘bar’][‘und']->content['baz']['foo']['bar'];

?>

{{ foo.bar.content.baz.foo.bar }}

Page 10: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Finding Stuff in TwigPrint all available variables

Print content of specific variable

{{ dump() }}

{{ dump(foo) }}

Page 11: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Loops

<h2>Organizers</h2><ul> {% for user in users %} <li>{{ user.username}}</li> {% endfor %}</ul>

<h2>Organizers</h2><ul> <li>David</li> <li>Peter</li> <li>Sean</li> </ul>

Page 12: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Loops (Cont.){{ loop.length }}

{{ loop.first }}

{{ loop.last }} {{ loop.index }}

{% if loop.first %} ... {% elseif loop.index == 2 %} ...{% elseif loop.last %} ...{% endif %}

Page 13: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Loop with Empty Text<h2>Organizers</h2><ul> {% for user in users %} <li>{{ user.username}}</li> {% else %} <li>no results found</li> {% endfor %}</ul>

Page 14: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Filter

<p> {% filter upper %} uppercase for the win {% endfilter %}</p>

Page 15: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

|Filter

{% set name = 'Sean' %}<span> {{ name|length }}</span>

<span> 4</span>

Page 16: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Filters Reference• abs

• batch

• capitalize

• convert_encoding

• date

• date_modify

• default

• escape

• first

• format

• join

• json_encode

• keys

• last

• length

• lower

• merge

• nl2br

• number_format

• raw

• replace

• reverse

• round

• slice

• sort

• split

• striptags

• title

• trim

• upper

• url_encode

Page 17: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Twig Blockspage.html.twig

page--front.html.twig

{% block headerblock %} <h2>DrupalCamp NJ</h2>{% endblock %}

{% extends "page.html.twig" %}{% block headerblock %} {{ parent() }} <h4>Fourth Annual</h4>{% endblock %}

Page 18: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Other Drupal 8 Theme Changes

Page 19: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

HTML5 + CSS3

Page 20: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Good RiddanceIE6

(<1%)IE7

(<1%)IE8

(~10%)

Page 21: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

CSS Built on SMACSS & BEM

CSS

HTML

.field { margin: 20px 15px;}.field.field—name { color: orange;}

<div class=“field field--name”>DCNJ</div>

Page 22: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Extra Bits• Themes in /themes folder

no more /sites/all/themes/…

• Templates are auto-loaded with hook_theme implementation key!

• Drupal 8 Theme Layer

Page 23: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

theme.info.yml• Formerly theme.info (same data)

• No more stylesheets or scripts properties(still have stylesheets-override & stylesheets-remove)

• Need to define CSS & JS in *.libraries.ymlwhy-slider: version: 1.x css: theme: css/why-slider.css: {} js: js/why-slider.js: {} dependencies: - core/jquery

Page 24: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Twig FTW!Drupal Specific Functionality

Page 25: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Filters{% set class_name = 'dcnj/2015' %}

{% set organizers = [ 'davidhernandez', 'pwolanin', ]%}

{{ class_name|clean_class }}{{ organizers|without('pwolanin') }},{{ attendees|placeholder('you') }}

dcnj-2015davidhernandezyou

Page 26: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Other MethodsaddClass / removeClass

setAttribute / removeAttribute<div{{ attributes.setAttribute('id', 'camp').setAttribute('I-Love', 'NJ') }}>

<div{{ attributes.removeAttribute('id') }}>

<div{{ attributes.addClass('field-item-' ~ name|clean_class) }}>

<div{{ attributes.removeClass('foo', 'bar').addClass('baz') }}>

Page 27: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Translate

or{% trans %} Author {{ username }}{% endtrans %}

{{ 'Author: @username'| t({'@username':username}) }}

Page 28: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Twig Debug

<!-- 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' -->

Enable in /sites/default/services.yml

Page 29: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Improving the Themer Experience

CONSENSUS BANANA

Phase 1 Move classes from preprocess to templates

Phase 2 Move templates from Core to the new Classy base theme

Page 30: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Consensus Banana

Page 31: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Phase 1 Example{% 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>

node.html.twig

Page 32: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Phase 2 - Classy

Page 33: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Why Classy

Page 34: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

Getting Involved• FREE Mentoring & Collaboration Day

Sunday, Feb. 1 @ Tigerlabs

• Monthly Mentoring & CollaborationThird Tuesday 7-9pm @ Tigerlabs

• IRC #drupal-twig #drupal-contribute

• Bi-weekly Twig Hangouts (alt. 7am/pm)Next is Thursday, Feb. 12 @ 7pm

Page 35: Drupal 8 Theming - Exploring Twig and Other Frontend Changes

CROWDCommunications Group, LLC

CROWDCommunications Group, LLC

Questions?Sean T. Walsh

[email protected]@seantwalsh @crowdcg

irc: crowdcg