Description
I am using Pattern Lab Node v3.0.4
on Mac
, with Node v10.16.3
, using a Custom
Edition.
Actual Behavior
Patterns can only be included using a single tag include:
{{> molecules-some-pattern }}
They can be parameterized using e.g. pattern parameters and pattern data. If the pattern you create is a customization of a general HTML div
tag, this limits ones options very much. For instance, I created an atom for a general section
on a web page:
This mimics the generated HTML for a section div
in Zoho Sites. Since this is a generic container, you can add any type and any amount of content. I made this pattern data driven via Handlebars dynamic partials to detect the content which needs to be put between the opening and closing div
section tag.
Suppose I want to have an organism pattern representing a section with a title and a two column layout as can be seen in the following example image:
To implement this complex nesting, you end up writing HTML in JSON:
https://gitlab.com/ringods/starterkit-zoho-sites/blob/03bbe04aba199266d9c28b4cb1b3479ea6df36c3/dist/_patterns/02-organisms/00-heading/00-section-114.hbs
https://gitlab.com/ringods/starterkit-zoho-sites/blob/03bbe04aba199266d9c28b4cb1b3479ea6df36c3/dist/_patterns/02-organisms/00-heading/00-section-114.json
I clearly went too far on the data-driven and dynamic partial approach here. Re-using the pattern in a template or page would mean I need to copy and embed a full JSON structure in the data file for the tempate or page.
This got me thinking on what I was missing in PL.
Expected Behavior
If a pattern could be written as a block, meaning an open and close tag, a molecule or organism could be much easier written by including lower level patterns. E.g. my section
organism representing the image could be written as:
{{! 02-organisms/00-heading/00-section-114.hbs }}
{{> atoms-section }}
{{> atoms-row}}
{{> atoms-column}}
{{#block title }}
{{#with title}}
{{> molecules-heading }}
{{/with}}
{{/block }}
{{/ atoms-column}}
{{/atoms-row}}
{{>atoms-row}}
{{>atoms-column}}
{{#each entries}}
{{! ... more content here to generate each entries' structure }}
{{/each}}
{{/atoms-column}}
{{/atoms-row}}
{{/ atoms-section }}
A corresponding data file for a section like written above could then be limited to the "true" data:
{
"title": {
"header": "Menu",
"align": "center",
"element": "h2"
},
"entries": [
{
"food": "Bread with Butter and Jam",
"description": "Warm oven baked bread with homemade preserves and butter",
"price": "€5"
},
{
"food": "Cereal",
"description": "Roasted flakes of grains with honey",
"price": "€3"
}
]
}
Steps to Reproduce/Emulate
I tried this by using the wax-on set of Handlebars helpers and registerd them like this in PL:
const wax = require('wax-on')
module.exports = function (Handlebars) {
wax.on(Handlebars);
wax.setLayoutPath('source/layouts')
};
I created the section as a wax-on
layout under source/layouts
:
{{! source/layouts/section.hbs }}
<div data-element-type="section"
class="zpsection zpdefault-section zpdefault-section-bg{{#if image}} zpbackground-size-cover zpbackground-position-center-center zpbackground-repeat-all zpbackground-attachment-scroll{{/if}}{{#if video}} zpvideo-section{{/if}}{{theme-section-classes}}"
{{#if image}}style="background-image:url({{image}});" {{/if}} {{#if color}}style="background-color:{{color}};"
{{/if}}>
<div class="zpcontainer">
{{#if video}}
<div class="zpvideo-bg-container">
<video class="zpsection-video" style="min-width:100%;min-height:100%;" src="{{video}}" muted="" loop=""
autoplay=""></video>
<div class="zpvideo-bg-overlay zpvideo-fallback-image" style="background-image:url();"></div>
<div class="zpvideo-bg-overlay"></div>
</div>
{{/if}}
{{#block content }}
{{/block}}
</div>
</div>
The organism using the section then reads this:
{{!organisms-section-114.hbs}}
{{#extends 'section'}}
{{#block content}}
{{#with title}}
{{> molecules-heading}}
{{/with}}
{{/block}}
{{/extends}}
The above snippet only displays the title, but more could be added to add the full menu as in the picture above. However, I don't like this syntax for one main reason: layouts are a wax-on term and are not integrated in Handlebars as patterns. I also can't document it as a pattern.
What do you think of the ability to write patterns as an open/close tag combination to allow for nested content much more easily?