drupal 8 introduction to theming

42
DRUPAL8 THEMING Braham Pal Singh Senior consultant, Capgemini

Upload: brahampal-singh

Post on 18-Aug-2015

15 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Drupal 8  introduction to theming

DRUPAL8 THEMINGBraham Pal Singh

Senior consultant, Capgemini

Page 2: Drupal 8  introduction to theming

INTRODUCTION

● What’s New in Drupal8

● D8 core structure

● D8 theme structure

● The Twig Template Engine

● Responsive Images

Page 3: Drupal 8  introduction to theming

What’s new in Drupal 8?

Page 4: Drupal 8  introduction to theming

Responsive Theming

Page 5: Drupal 8  introduction to theming

HTML5 out of the box

Page 6: Drupal 8  introduction to theming

Views in Core

Page 7: Drupal 8  introduction to theming

Views = fully customizable...With the Drupal 8 views, you can

customize:– Admin listings – Sidebar content – Image galleries – Slideshows – REST output

Page 8: Drupal 8  introduction to theming

Restyled administration interface

Page 9: Drupal 8  introduction to theming

New front-end libraries

Page 10: Drupal 8  introduction to theming

The Classy Base Theme• core/themes/classy• Used by Bartik & Seven• Declare in theme.info.yml: base theme: classy

Page 11: Drupal 8  introduction to theming

The D8 core structure●root/themes

●root/modules

●root/core/themes

●root/core/modules

Page 12: Drupal 8  introduction to theming

The D8 theme structure●themename.info.yml

●themename.libraries.yml

●themename.theme

●css folder

●templates folder

●sass folder

●img folder

●js folder

Page 13: Drupal 8  introduction to theming
Page 14: Drupal 8  introduction to theming

Create an .info.yml file

DRUPAL 7 DRUPAL 8

Page 15: Drupal 8  introduction to theming

theme.info.ymlname: [themename]type: themedescription: This is my custom Drupal 8 Themecore: 8.xengine: twigscreenshot: screenshot.pngstylesheets: all: - css/styles.cssregions: header: ‘Header’...

libraries: - [themename]/[namespace]

Page 16: Drupal 8  introduction to theming

theme.libraries.yml

[namespace]: version: 1.x js: js/foo.js

Page 17: Drupal 8  introduction to theming

What is TWIG ??

Page 18: Drupal 8  introduction to theming

What is Twig?Twig is a template framework that has replaced

PHPTemplate in Drupal 8.

fast, secure and flexible.

http://twig.sensiolabs.org/documentation

Page 19: Drupal 8  introduction to theming

TWIG

IDE integration

Page 20: Drupal 8  introduction to theming

TWIG

recognizable syntax

Page 21: Drupal 8  introduction to theming

by the creator of Symfony Fabien

Potencier

TWIG

Page 22: Drupal 8  introduction to theming

Intro to Twig

TWIG

There are three delimeters you need to know.

● Print variables: {{ … }}

● Comments: {# … #}

● Execute statements: {% … %}

Page 23: Drupal 8  introduction to theming

TWIG

● Variables can be accessed directly in Twig.

● Classes can be drilled into to show attributes:

<div {{ attributes }}>

{{ content }}

</div>

● Conditional statements: {% if … %}{% endif %}

● Loops: {% for … %}{% endfor %}

● Blocks:

{% block blockname %}...{% endblock %}

● Extends:

{% extends ‘.....’ %}

Page 24: Drupal 8  introduction to theming

TWIGPHPTemplat

eTwig

Page 25: Drupal 8  introduction to theming

what does it look like?

TWIG

Page 26: Drupal 8  introduction to theming

print with {{ }}

TWIG

Page 27: Drupal 8  introduction to theming

commands with {% %}

TWIG

Page 28: Drupal 8  introduction to theming

comments with {# #}

TWIG

Page 29: Drupal 8  introduction to theming

drill down into all variables with .(dot)

TWIG

Page 30: Drupal 8  introduction to theming

less code than PHP functions

TWIG

theme_image becomes image.html.twig

Page 31: Drupal 8  introduction to theming

less code than PHP functions

TWIG

theme_username becomes username.html.twig

Page 32: Drupal 8  introduction to theming

TWIG

• PHP template reads files from disk on every use

Twig templates are read once & compiled into classes

Rendering should get much faster when the same

content element appears multiple times on the page.

Consolidating many similar templates will result in an additional gain.

Possible performance gains

Page 33: Drupal 8  introduction to theming

TWIG

• Saving PHP code in the database is a HUGE no-no.

• Twig is not PHP, and is safe to store!

In-browser template editing finally safe.

Page 34: Drupal 8  introduction to theming

TWIG

One template can return markup for both your PHP generated pages, as well as pages generated vi JS in AJAX callbacks.

We can use other open source libraries like TwigJS.

(https://github.com/schmittjoh/twig.js)

Security• Drupal 7 would often print out unsanitized data. You could

even run database queries from directly inside the theme files.

• Drupal 8 can automatically sanitize variables and won't permit unsafe functions to run.

Twig template files can be used on the front end, too.

Page 35: Drupal 8  introduction to theming

TWIGWhy Twig in Drupal8?

Drupal 7: Drupal 8:

Page 36: Drupal 8  introduction to theming

D8 IMPROVEMENTS

remove theme functions (and overrides) entirely.

remove process.

Page 37: Drupal 8  introduction to theming

D8 IMPROVEMENTS

remove render. remove page alter?

Page 38: Drupal 8  introduction to theming

Drupal 8 Template FilesDrupal 7 node.tpl.php file: Drupal 8 node.html.twig file:

Page 39: Drupal 8  introduction to theming

THEME.schema.yml

Page 40: Drupal 8  introduction to theming

Theme functions & preprocess

Page 41: Drupal 8  introduction to theming

Responsive Images

• theme.breakpoints.ymlEnable the Responsive images

module

Page 42: Drupal 8  introduction to theming

Thank you!

Questions?