Hosted by Anthony Simone
asimone@elevatedthird.com
At its core, Pattern Lab is a static site generator (powered by either PHP or Node) that stitches together UI components. But there's a whole lot more to it than that!
Maintain a Drupal 8 project that implements Pattern Lab. All assets (templates, CSS, JS) are shared between the two projects in an identical state (one source).
.twig
files
<article class="product">
<div class="main">
{% include "atoms-title" %}
{% include "atoms-landscape-16x9" %}
{% include "atoms-body" %}
</div>
<aside class="details">
{% include "molecules-details" %}
</aside>
</article>
If you're thinking: "Wait, but that's no good!", you're right, it's not!
{% include "atoms-title" %}
!= {{ content.field_title }}
.twig
templates and .json
files exist in pairs.json
data to include templatesAttribute()
- Compiles the Drupal attributes objectinclude()
- Include a template in the specified variablejoin()
- Join multiple templates together in the specified variable{{ content.field_name }}
or {{ content }}
vars.json
"attributes": {
"Attribute()": {
"class": ["class-name"]
}
}
.twig
<article{{ attributes }}>
{# ...drupal template content... #}
</article>
.json
{
"label": "Jon Doe",
"content": {
"field_profile_image": {
"include()": {
"pattern": "elements-profile-image"
}
}
}
}
.twig
<article>
{{ label }}
{{ content.field_profile_image }}
</article>
.json
{
"page": {
"content_bottom": {
"join()": [
"elements-efq-featured-event",
"elements-efq-upcoming-events",
"elements-efq-past-events"
]
}
}
}
.twig
<div class="page-template">
{# ... #}
<div class="region-content-bottom">
{{ page.content_bottom }}
</div>
{# ... #}
</div>
Progress! But, we still must practically get Drupal and Pattern Lab working together.
Let's look at some more challenges that come up for an implementation of Pattern Lab in Drupal and see how our approach will solve them
Drupal 8 Twig | Pattern Lab Twig |
---|---|
Finds templates anywhere in the Drupal theme | Recognizes in specific locations within the PL project |
Only recognizes templates ending with .html.twig |
Any templates ending in .twig |
Will not include .twig files |
PL is inside our Drupal project |
/theme/templates/node/node--news--teaser.html.twig
{% set classes = [
'node',
'node--type-' ~ node.bundle|clean_class,
not node.isPublished() ? 'node--unpublished',
view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
] %}
<article{{ attributes.addClass(classes) }}>
<div{{ content_attributes.addClass('node__content') }}>
{{ content.field_date }}
{{ content.field_link }}
</div>
</article>
.../_pattern/03-nodes/09-news/02-news-teaser.twig
{% extends "node--news--teaser.html.twig" %}
.../_pattern/03-nodes/09-news/02-news-teaser.json
{
...
"attributes": {
"Attribute()": {
"class": []
}
},
"node": {
"bundle": "news"
},
"view_mode": "teaser",
"content": {
"field_link": {
"include()": {
"pattern": "elements-field-link"
}
},
"field_date": {
"include()": {
"pattern": "elements-field-date",
"with": {
"items": [
{
"content": ""
}
]
}
}
}
}
}
{{ content }}
{{ content }}
worksjoin()
can merge multiple field templates into a string and print them all out with {{ content }}
{{ content.field_name }}
include()
single templates in each key under content and print them with {{ content.field_name}}
pattern-lab/source/_twig-components
public
dir (compilation target)published
directoryMaintain a Drupal 8 project that implements Pattern Lab. All assets (templates, CSS, JS) are shared between the two projects in an identical state (one source).
Permits for a frontend to be designed and built out before a sitebuild must be committed to, without "losing" that time
Isolates components by responsibility outside of the Drupal context (wrappers, containers, other element classes, etc)
Produces a comprehensive interactive styleguide asset that develops with the project requiring minimal update
Provides an entire set of data for frontend design system that does not depend on the project database
Quick (relatively) visual "regression testing" for complicated components, or items that have many dependencies and moving parts
Connecting communities! Pattern Lab is an existing tool with a lot of dedicated followers. And with the hard work of a few people, a bridge to connect Drupal with this large existing community has been made. That's pretty cool!
composer create-project pattern-lab/edition-drupal-standard
pattern-lab/config/config.yml
set twigAutoescape: false
pattern-lab/source/_meta/_00-head.twig
)pattern-lab/source/_patterns/08-drupal-templates
and create symlinks inside to all of your direct template containing foldersWhat did you think?
Locate this session at the DrupalCon website