Branch Out of Your Comfort Zone with Twig Templates Larry - - PowerPoint PPT Presentation

branch out of your comfort zone with twig templates larry
SMART_READER_LITE
LIVE PREVIEW

Branch Out of Your Comfort Zone with Twig Templates Larry - - PowerPoint PPT Presentation

Branch Out of Your Comfort Zone with Twig Templates Larry Walangitan @larrywalangitan http://chromatichq.com @ChromaticHQ PHPTemplate Twig PHPTemplate vs Twig PHPTemplate vs Twig // node.tpl.php <?php if (!$page): ?> <h2<?php


slide-1
SLIDE 1

Branch Out of Your Comfort Zone with Twig Templates

slide-2
SLIDE 2

Larry Walangitan

@larrywalangitan

slide-3
SLIDE 3

@ChromaticHQ http://chromatichq.com

slide-4
SLIDE 4

PHPTemplate

slide-5
SLIDE 5

Twig

slide-6
SLIDE 6

PHPTemplate vs Twig

slide-7
SLIDE 7

PHPTemplate vs Twig

// node.tpl.php <?php if (!$page): ?> <h2<?php print $title_attributes; ?>> <a href="<?php print $node_url; ?>"><?php print $title; ?></a> </h2> <?php endif; ?> // node.html.twig {% if not page %} <h2{{ title_attributes.addClass('node__title') }}> <a href="{{ url }}”> {{ label }}</a> </h2> {% endif %}

slide-8
SLIDE 8

Creating Templates Overriding Default Templates Theme Hook Suggestions Naming Conventions Variables Conditionals

Roadmap

Control Structures Filters Links Render Arrays Debugging Coding Standards

slide-9
SLIDE 9

Creating Templates

slide-10
SLIDE 10

Creating Templates: Drupal 7

aggregator-feed- source.tpl.php aggregator-item.tpl.php aggregator-summary- item.tpl.php aggregator-summary- items.tpl.php aggregator-wrapper.tpl.php block-admin-display- form.tpl.php block.tpl.php book-all-books-block.tpl.php book-export-html.tpl.php book-navigation.tpl.php book-node-export- html.tpl.php comment-wrapper.tpl.php comment.tpl.php field.tpl.php forum-icon.tpl.php forum-list.tpl.php forum-submitted.tpl.php forum-topic-list.tpl.php forums.tpl.php node.tpl.php

  • verlay.tpl.php

poll-bar--block.tpl.php poll-bar.tpl.php poll-results--block.tpl.php poll-results.tpl.php poll-vote.tpl.php profile-block.tpl.php profile-listing.tpl.php profile-wrapper.tpl.php search-block-form.tpl.php search-result.tpl.php search-results.tpl.php page.tpl.php maintenance-page.tpl.php region.tpl.php html.tpl.php axonomy-term.tpl.php toolbar.tpl.php user-picture.tpl.php user-profile-category.tpl.php user-profile-item.tpl.php user-profile.tpl.php

in `modules/*/..`

slide-11
SLIDE 11

Creating Templates: Drupal 7

function THEME_preprocess_node(&$variables) { if ($variables['promote']) { $variables['theme_hook_suggestions'] = 'node__promoted'; } }

* node.tpl.php * node--promoted.tpl.php template.php

slide-12
SLIDE 12

Creating Templates: Drupal 8

| theme | - templates | | - block.html.twig | | - comment.html.twig | | - maintenance-page.html.twig | | - node.html.twig | | - page.html.twig | | - status-messages.html.twig | | - custom-template.html.twig

theme/templates/*.html.twig

slide-13
SLIDE 13

Overriding Default Templates

slide-14
SLIDE 14

Overriding Default Templates: Drupal 7

function THEME_preprocess_page(&$variables) { if (isset($variables['node']->type)) { $variables['theme_hook_suggestions'][] = $variables[‘node']->type; } }

* page.tpl.php * page—[node-type].tpl.php template.php

slide-15
SLIDE 15

Overriding Default Templates: Drupal 8

1. Locate the template you wish to override.

  • 2. Copy the template file from its base location into your theme folder.
  • 3. (optionally) Rename the template according to the naming conventions.
  • 4. Modify the template to your liking.

“ ”

slide-16
SLIDE 16

Theme Hook Suggestions

slide-17
SLIDE 17

Theme Hook Suggestions: Drupal 7

/** * Implements hook_preprocess_HOOK. */
 function THEME_preprocess_page(&$variables) { if (isset($variables['node']->type)) { $variables['theme_hook_suggestion'] = $variables[‘node']->type; } }

template.php

slide-18
SLIDE 18

Theme Hook Suggestions: Drupal 7

$variables['theme_hook_suggestion'] != $variables['theme_hook_suggestions']

template.php

slide-19
SLIDE 19

Theme Hook Suggestions: Drupal 8

function hook_theme_suggestions_alter(array &$suggestions, array &$variables, $hook) { // Alters named suggestions for all theme hooks. } function hook_theme_suggestions_HOOK(array &$variables) { // Provides alternate named suggestions for a specific theme hook. } function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) { // Alters named suggestions of a specific theme hook. // Can reorder or remove suggestions provided by hook_theme_suggestions_HOOK. }

custom.theme

slide-20
SLIDE 20

Theme Hook Suggestions: Drupal 8

/** * Implements hook_theme_suggestions_HOOK_alter(). */ function custom_suggestions_user_alter(array &$suggestions, array &$variables) {

if (!empty($variables['elements']['#view_mode'])) {

$suggestions[] = 'user__' . $variables['elements']['#view_mode']; } }

custom.theme user--custom-view-mode.html.twig

slide-21
SLIDE 21

Naming Conventions

slide-22
SLIDE 22

Naming Conventions: Drupal 7

# Naming Conventions Hierarchy * page--front.tpl.php // but only if node/1/edit is the front page * page--node--1.tpl.php // prefix is not changed because the component is a number * page--node--%.tpl.php * page--node.tpl.php // and prefix is set to page__node * page.tpl.php // this is always a suggestion

http://example.com/node/1

slide-23
SLIDE 23

Naming Conventions: Drupal 7

# Naming Conventions Hierarchy * page--front.html.twig // but only if node/1/edit is the front page * page--node--1.html.twig * page--node.html.twig * page.html.twig

http://example.com/node/1

slide-24
SLIDE 24

Variables

slide-25
SLIDE 25

Variables: Drupal 7

template.php

function THEME_preprocess_node(&$variables) { $variables['foo'] = 'bar'; }

node.tpl.php

<div id="foo-wrapper"> <?php print $foo; ?> </div>

slide-26
SLIDE 26

Variables: Drupal 8

custom.theme

function custom_preprocess_node(&$variables) { $variables['foo'] = 'bar'; }

<div id="foo-wrapper"> {{ foo }} </div>

node.html.twig

slide-27
SLIDE 27

Variables: Comparison

function custom_preprocess_node(&$variables) { $variables['foo'] = 'bar'; }

template.php || custom.theme node.tpl.php

<div id="foo-wrapper"> {{ foo }} </div>

node.html.twig

<div id="foo-wrapper"> <?php print $foo; ?> </div>

slide-28
SLIDE 28

Conditionals

slide-29
SLIDE 29

Conditionals: Drupal 7

<?php if ($url): ?> <a href="<?php print $url; ?>"><?php print $image; ?></a> <?php else: ?> <?php print $image; ?> <?php endif; ?>

slide-30
SLIDE 30

Conditionals: Drupal 8

{% if url %} <a href="{{ url }}">
 {{ image }} {% else %} {{ image }} {% endif %}

slide-31
SLIDE 31

Conditionals: Comparison

{% if url %} <a href="{{ url }}">
 {{ image }} </a> {% else %} {{ image }} {% endif %} <?php if ($url): ?> <a href="<?php print $url; ?>"> <?php print $image; ?> </a> <?php else: ?> <?php print $image; ?> <?php endif; ?>

PHPTemplate Twig

slide-32
SLIDE 32

Conditionals: Comparison

// !empty() {% if url is not empty %} <a href="{{ url }}"> My Link! </a> {% endif %} // isset() {% if url is defined %} <a href="{{ url }}"> My Link! </a> {% endif %} // !empty() <?php if !empty($url): ?>
 <a href="<?php print $url; ?>”> My Link! </a>
 <?php endif; ?> // isset() <?php if isset($url): ?>
 <a href="<?php print $url; ?>”> My Link! </a>
 <?php endif; ?>

PHPTemplate Twig

slide-33
SLIDE 33

Control Structures

slide-34
SLIDE 34

Control Structures: Drupal 7

foreach ($rows as $row) { print $row; } <?php foreach ($rows as $row): ?> <?php print $row; ?> <?php endforeach; ?>

PHP PHPTemplate

slide-35
SLIDE 35

Control Structures: Drupal 8

{% for row in rows %}
 {{ row.content }} {% endfor %}

slide-36
SLIDE 36

Control Structures: Comparison

PHPTemplate Twig

{% for row in rows %}
 {{ row.content }} {% endfor %} <?php foreach ($rows as $row): ?> <?php print $row; ?> <?php endforeach; ?>

slide-37
SLIDE 37

Filters

slide-38
SLIDE 38

Filters

  • Check Plain
  • Translate
  • Translate with substitutions
  • Implode
  • Escape
slide-39
SLIDE 39

Filters: Drupal 7

// Check Plain <?php print check_plain($title); ?> // Translate <?php print t('Home'); ?> // Translate with Substitutions <?php print t('Welcome, @username', array('@username' => $user->name)); ?> // Implode <?php echo implode(', ',$usernames); ?> // Escape <?php echo check_plain($title); ?>

slide-40
SLIDE 40

Filters: Drupal 8

// Check Plain {{ title|striptags }} // Translate {{ 'Home'|t }} // Translate with Substitutions {{ 'Welcome, @username'|t({ '@username': user.name }) }} // Implode {{ usernames|safe_join(', ') }} // Escape {{ title }}

slide-41
SLIDE 41

Filters: Comparison

// Check Plain {{ title|striptags }} // Translate {{ 'Home'|t }} // Translate with Substitutions {{ 'Welcome, @username'| t({ '@username': user.name }) }} // Implode {{ usernames|safe_join(', ') }} // Escape {{ title }}

// Check Plain <?php print check_plain($title); ?> // Translate <?php print t('Home'); ?> // Translate with Substitutions <?php print t('Welcome, @username', array('@username' => $user->name)); ?> // Implode <?php echo implode(', ',$usernames); ?> // Escape <?php echo check_plain($title); ?>

PHPTemplate Twig

slide-42
SLIDE 42

Filters: Drupal 8

// Implode and lowercase {{ usernames|safe_join(', ')|lower }}

slide-43
SLIDE 43

Links

slide-44
SLIDE 44

Links: Drupal 7

drupal_get_path() // returns path of a system item. path_to_theme() // returns path to the current themed element

Template.php

$variable['custom_url'] = url(node_uri($node)['path']);

custom-url.tpl.php

<a href="<?php print $custom_url; ?>”> Check out this link! </a>

slide-45
SLIDE 45

Links: Drupal 8

path() - Generates a [relative] URL path given a route name and parameters.
 // Link to the default frontpage content listing view.
 <a href="{{ path('view.frontpage'); }}"> {{ 'View all content'|t }} </a>
 
 
 url() - Generates an absolute URL given a route name and parameters.
 // Link to a specific node page. <a href="{{ url('entity.node.canonical', {'node': node.id}) }}"> {{ 'Read more'|t }} </a>

slide-46
SLIDE 46

Render Arrays

slide-47
SLIDE 47

Render Arrays: Drupal 7

// Using theme() in D7. $logo = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); // Render array in D7. $logo = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'),
 );

template.php page.tpl.php

<?php print $logo; ?>

page.tpl.php

<?php print render($logo); ?>

slide-48
SLIDE 48

Render Arrays: Drupal 8

{{ logo }} // Render array in D8. $logo = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'),
 );

custom.theme page.html.twig

slide-49
SLIDE 49

Render Arrays: Comparison

{{ logo }} // Render array in D7/D8. $logo = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'),
 );

template.php || custom.theme page.html.twig page.tpl.php

<?php print render($logo); ?>

slide-50
SLIDE 50

Render Array Resources

Blog Post https://chromatichq.com/blog/badcamp-2015-transitioning-theme-and-theme-functions- render-arrays-and-templates DrupalCon Session https://events.drupal.org/neworleans2016/sessions/aha-understanding-and-using-render- arrays-drupal-8

slide-51
SLIDE 51

Debugging

slide-52
SLIDE 52

Debugging: Drupal 7

// template.php dpm($variables); // Settings.php $conf['theme_debug'] = TRUE;

Devel Module & Devel Themer

slide-53
SLIDE 53

Debugging: Drupal 8

parameters: twig.config: debug: true auto_reload: true

sites/default/services.yml

// *.html.twig {{ kint() }}

Devel Module *.html.twig

// all variables in the current context. {{ dump() }} // a specific variable. {{ dump(variable) }}

slide-54
SLIDE 54

Coding Standards

slide-55
SLIDE 55

Coding Standards: Drupal 7

* Add 2 spaces for indents. * Match the indentation of long opening and closing block HTML tags. * Avoid curly brackets. * Prefer PHP in HTML to HTML in PHP in templates. * Separate logic from presentation. * Avoid Drupal functions in templates. * Always put a semicolon at the end of your small PHP printing statement

slide-56
SLIDE 56

Coding Standards: Drupal 8

* Indent code inside of tags. * Only use lowercase and underscored variable names.

slide-57
SLIDE 57

Coding Standards: Drupal 8

* Delimiters (beginning and end) {{, {&, {#
 }}, %}, #}

Place only one space Before:

* Comparison operators * Logic * Ternary operators

Before and After:

* : sign in hashes * , in arrays & hashes

After:

slide-58
SLIDE 58

Coding Standards: Drupal 8

* Closing parenthesis in an expression: {{, {&, {#
 }}, %}, #}

No Spaces Before:

* String delimiters ‘ & “ * The following operators:
 | . .. [] * Parenthesis in filter/function calls

Before and After:

* Opening parenthesis in an expression * , in arrays and hashes

After:

slide-59
SLIDE 59

Join us for contribution sprints

  • n Friday, September 30

First Time Sprinter Workshop - 9:00-12:00 - Room Wicklow2A Mentored Core Sprint - 9:00-18:00 - Wicklow Hall 2B General Sprints - 9:00 - 18:00 - Wicklow Hall 2A

slide-60
SLIDE 60

Q & A

slide-61
SLIDE 61

Branch Out of Your Comfort Zone with Twig Templates

Evaluate the Session:

https://events.drupal.org/dublin2016/sessions/branch-out- your-comfort-zone-twig-templates