by Tamás Hajas
Drupal / Web Project Manager &
Front-end Developer @
Integral Vision Ltd
{% extends "@stable/field/field.html.twig" %}
{% set myclass field_name|clean_class %}
{% set attributes = attributes.addClass(myclass) %}
themes/custom/mytheme/templates/field.html.twig
in node--article.html.twig
{% extends "node.html.twig" %}
{% block node_head %}
{% block node_title %}
{# Display title #}
{% endblock node_title %}
{% block node_submitted %}
{# Display author & date #}
{% endblock node_submitted %}
{% endblock node_head %}
in node--article--teaser.html.twig
{% extends "node--article.html.twig" %}
{% block node_head %}
{% block node_submitted %}
{# Display date only #}
{% endblock node_submitted %}
{% block node_title %}
{{ parent() }}
{% endblock node_title %}
{% endblock node_head %}
{% include … %}
{% embed %}
{% macro %}
, {% use %}
, etc.
<article class=”node node--type-article node--view-mode-full”>
…give rise to css like
.node--type-article.node--view-mode-full {
// Article styles here
}
.node--type-testimonial.node--view-mode-full {
// Testimonial styles here
}
<article class=”article article--display-full node”>
…give rise to css like
.article {
// Global article styles here
}
.article--layout-full {
// Modifier styles for the full page
}
.testimonial {
// Global article styles here
}
.testimonial--layout-full {
// Modifier styles for the full page
}
in node.html.twig
{% set bundle = node.bundle|clean_class %}
{%
set classes = [
bundle,
view_mode ? bundle ~ '--display-' ~ view_mode|clean_class,
node.isPromoted() ? bundle ~ '--promoted',
node.isSticky() ? bundle ~ '--sticky',
not node.isPublished() ? bundle ~ '--unpublished',
'node',
]
%}
<article{{ attributes.addClass(classes) }}>
in block.html.twig
{% if content.body['#bundle'] is defined %}
{% set block_bundle = content.body['#bundle'] %}
{% endif %}
{%
set classes = [
block_bundle ? 'block--' ~ block_bundle|clean_class,
'block',
'block-' ~ configuration.provider|clean_class,
'block-' ~ plugin_id|clean_class,
]
%}
<div{{ attributes.addClass(classes) }}>
Example output
in themes/custom/mytheme/mytheme.theme
/** Implements hook_preprocess_HOOK() for field.html.twig.
*/
function mybartik_preprocess_field(&$variables, $hook) {
$variables['bundle'] = $variables['element']['#bundle'];
}
in themes/custom/mytheme/templates/field.html.twig
{% set classes = [
bundle ~ '__'~ field_name|replace({'field_' : ''})|clean_class
] %}
{% set attributes = attributes.addClass(classes) %}
<p>
{{ 'All rights reserved 2015–'|t }}{{ 'now'|date('Y') }}.
</p>
{% set current_date = date()|date('U')|format_date('short') %}
<p>
{% trans %}
The current date is {{ current_date|placeholder|striptags }}
{% endtrans %}
</p>
{% if node.created.value and node.changed.value is defined %}
{% set node_pub = node.created.value|format_date() %}
{% set node_upd = node.changed.value|format_date() %}
<p>
{% trans %}
Published: {{ node_pub|placeholder|striptags }},
last updated: {{ node_upd|placeholder|striptags }}
{% endtrans %}
</p>
{% endif %}
*in page.html.twig
Read my article! ;)
Find 'em in my theme example!