Presentation by Anne Tomasevich
This is the part where I talk about myself
Photo by Jessie Gladdek
Themes allow you to change the look and feel of your Drupal site.
- Someone who's good at being very general
...and how do I, a person who probably doesn't like change, cope with these changes...
template.php
becomes [theme-name].theme
. (Dear Drupal gods, can we please have theme.php
?)For a more comprehensive list, visit Drupal's change log.
* I mean kinda ¯\_(ツ)_/¯
<?php db_query('DROP TABLE {users}'); ?>
Scary example courtesy of sqndr at d8.sqndr.com.
A fun note on the 8.x branch of a popular contrib theme:
This is a development branch. Please note that this branch is probably severely broken. It is strongly recommended to wait before using this branch or even creating issues regarding it.
Of the documentation that DOES exist, much of it is:
modules
and themes
folders, respectively.custom
folder for your custom theme!
# mappy.info.yml
name: Mappy
type: theme
description: 'D8 Theme for a basic leaflet site.'
core: 8.x
base theme: classy
libraries:
- mappy/global-styling
- mappy/leaflet
regions:
navbar: 'Top Navigation Bar'
content: Content # required!
sidebar: 'Sidebar'
footer: 'Footer'
[theme-name].info
becomes [theme-name].info.yml
# mappy.info.yml
name: Mappy
type: theme
description: 'D8 Theme for a basic leaflet site.'
core: 8.x
regions:
navbar: 'Top Navigation Bar'
content: Content # required!
sidebar: 'Sidebar'
footer: 'Footer'
This looks familiar, right?
# mappy.info.yml
base theme: classy
# mappy.info.yml
libraries:
- mappy/global-styling
- mappy/leaflet
*.libraries.yml
file.
# mappy.libraries.yml
global-styling:
css:
theme:
css/styles.css: {}
leaflet:
css:
theme:
css/leaflet.css: {}
js:
js/leaflet.js: {}
js/map.js: {}
dependencies:
- core/jquery
Note that jQuery is listed as a dependency of the Leaflet library. Since jQuery is no longer loaded automatically on every page, it must be explicitly required.
# mappy.info.yml
libraries:
- mappy/global-styling
- mappy/leaflet
# mappy.breakpoints.yml
mappy.mobile:
label: mobile
mediaQuery: '(min-width: 0px)'
weight: 2
multipliers:
- 1x
mappy.narrow:
label: narrow
mediaQuery: 'all and (min-width: 560px) and (max-width: 850px)'
weight: 1
multipliers:
- 1x
mappy.wide:
label: wide
mediaQuery: 'all and (min-width: 851px)'
weight: 0
multipliers:
- 1x
Ours is stolen adapted from the Bartik theme.
.breakpoints.yml
file (and uninstall and reinstall your theme), the breakpoints you've set will be exposed in the admin UI.{{ These }}
are for printing content, either explicitly or via functions
{% These %}
are for executing statements
{# These #}
are for comments
In D7 we render content like so:
<?php print render($page['sidebar']); ?>
Printing variables in D8 is as easy as including them inside double curly braces:
{# In page--front.html.twig #}
{# Print the sidebar region. #}
{{ page.sidebar }}
If a variable name contains special characters, use Twig's subscript syntax (which will look familiar to seasoned Drupalers).
{# In page--front.html.twig #}
{# Print the page type. #}
{{ page['#type'] }}
This will come in handy more during debugging.
Twig comes with many built-in filters that variables are passed to via the pipe character.
Here's the date filter:
{# Format the post date. #}
{{ post.published|date("Y-m-d") }}
Check them out here.
Remember our old friend t()?
{# Run an ARIA label through t() #}
<nav class="tabs" role="navigation" aria-label="{{ 'Tabs'|t }}">
Twig also comes with various functions that can be used in double curly braces.
Here's the cycle function doing some very important work:
{% set fruits = ['apple', 'orange', 'banana'] %}
{% for i in 0..10 %}
{{ cycle(fruits, i) }}
{% endfor %}
Used for control flow and other fun things.
{# From Bartik's page.html.twig #}
{# If there are tabs, output them. #}
{% if tabs %}
{% endif %}
In template files, we’ll use the if
statement quite often.
Make sure you check out the Twig coding standards!
To enable debug mode and turn off caching, we need to do 3 things:
You do NOT need turn off Twig caching - turning on auto reload is enough.
sites/example.settings.local.php
to sites/default
and rename to settings.local.php
. Fill out your local database settings.sites/default/settings.php
:
if (file_exists(__DIR__ . '/settings.local.php')) {
include __DIR__ . '/settings.local.php';
}
Check out settings.local.php
// In settings.local.php
/**
* Enable local development services.
*/
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
This tells us to head over to development.services.yml
.
// In sites/development.services.yml
parameters:
twig.config:
debug: true
auto-reload: true
Just kidding, you already did.
// In settings.local.php
/**
* Disable the render cache (this includes the page cache).
*
* This setting disables the render cache by using the Null cache back-end
* defined by the development.services.yml file above.
*
* Do not use this setting until after the site is installed.
*/
$settings['cache']['bins']['render'] = 'cache.backend.null';
Now you can:
With Twig debugging turned on, we can use the Twig function dump()
.
{# In a template file #}
{# Print out all variables on the page. #}
{{ dump() }}
{# Print the page's base path. #}
{{ dump(base_path) }}
Install the Devel and Devel Kint modules to use kint()
in the same way, but with a pretty, expandable array instead.
{# In a template file #}
{# Print out all variables on the page. #}
{{ kint() }}