The Node version of Pattern Lab is, at its core, a static site generator. It combines platform-agnostic assets, like the Mustache-based patterns and the JavaScript-based viewer, with a Node-based "builder" that transforms and dynamically builds the Pattern Lab site. By making it a static site generator, the Node version of Pattern Lab strongly separates patterns, data, and presentation from build logic. The Node version is a work in progress, the PHP version should be seen as a reference for other developers to improve upon as they build their own Pattern Lab Builders in their language of choice.
To run patternlab-node, run the following from the command line at the root of whichever directory you want to initialize your project in:
npm install
npm install -g grunt-cli
grunt
This creates all patterns, the styleguide, and the pattern lab site.
The following are grunt task arguments you may execute:
With no arguments, patternlab runs the full builder, compiling patterns, and constructing the front-end site.
Compile the patterns only, outputting to ./public/patterns
Retrieve the version of patternlab-node you have installed
Get more information about patternlab-node, pattern lab in general, and where to report issues.
To have patternlab-node watch for changes to either a mustache template, data, or stylesheets, run grunt watch
. The Gruntfile
governs what is watched. It should be easy to add scss or whatever preprocessor you fancy.
If you don't have a need for some of the nav-bar tools in the patternlab frontend, you can turn them off in config.json
.
The current selection is as follows. It reflects support versus patternlab-php.
"ishControlsVisible": {
"s": true,
"m": true,
"l": true,
"full": true,
"random": true,
"disco": true,
"hay": true,
"mqs": false,
"find": false,
"views-all": true,
"views-annotations": true,
"views-code": true,
"views-new": true,
"tools-all": true,
"tools-follow": false,
"tools-reload": false,
"tools-shortcuts": false,
"tools-docs": true
}
You can set the state of a pattern by including it in config.json
too. The out of the box styles are in progress (orange), in review (yellow), and complete (green).
Pattern states should be lowercase and use hyphens where spaces are present.
"patternStates": {
"colors" : "inprogress",
"fonts" : "inreview",
"three-up" : "complete"
}
Pseudo-patterns are meant to give developers the ability to build multiple and unique rendered patterns off of one base pattern and its mark-up while giving them control over the data that is injected into the base pattern. This feature is especially useful when developing template- and page-style patterns.
Pseudo-patterns are, essentially, the pattern-specific JSON files that would accompany a pattern. Rather than require a Mustache pattern, though, pseudo-patterns are hinted so a developer can reference a shared pattern. The basic syntax:
patternName~pseudoPatternName.json
The tilde, ~
, and JSON extension denotes that this is a pseudo-pattern. patternName
is the parent pattern that will be used when rendering the pseudo-pattern. patternName
and pseudoPatternName
are combined when adding the pseudo-pattern to the navigation.
The JSON file itself works exactly like the pattern-specific JSON file. It has the added benefit that the pseudo-pattern will also import any values from the parent pattern's pattern-specific JSON file. Here is an example (which ships with the package) where we want to show an emergency notification on our homepage template. Our 03-templates/
directory looks like this:
00-homepage.mustache
01-blog.mustache
02-article.mustache
Our 00-homepage.mustache
template might look like this:
<div id="main-container">
{{# emergency }}
<div class="emergency">Oh Noes! Emergency!</div>
{{/ emergency }}
{ ...a bunch of other content... }
</div>
If our _data.json
file doesn't give a value for emergency
that section will never show up when 00-homepage.mustache
is rendered.
We want to show both the regular and emergency states of the homepage but we don't want to duplicate the entire 00-homepage.mustache
template. That would be a maintenance nightmare. So let's add our pseudo-pattern:
00-homepage.mustache
00-homepage~emergency.json
01-blog.mustache
02-article.mustache
In our pseudo-pattern, 00-homepage~emergency.json
, we add our emergency
attribute:
{
"emergency": true
}
Now when we generate our site we'll have our homepage template rendered twice. Once as the regular template and once as a pseudo-pattern showing the emergency section. Note that the pseudo-pattern will show up in our navigation as Homepage Emergency
.
You can build patterns that link to one another to help simulate using a real website. This is especially useful when working with the Pages and Templates pattern types. The basic format is:
{{ link.pattern-name }}
For example, if you wanted to add a link to the home page
template from your blog
template you could write the following:
<a href="{{ link.templates-homepage }}">Home</a>
This would compile to:
<a href="/patterns/templates-homepage/templates-homepage.html">Home</a>
As you can see, it's a much easier way of linking patterns to one another.
config.json
also has two properties that work together to export completed patterns for use in a production environment. Provide an array of keys and an output directory. Pattern Lab doesn't ship with any pattern export keys, but the default directory is "./pattern_exports/"
created inside the install directory.
"patternExportKeys": ["molecules-primary-nav", "organisms-header", "organisms-header"],
"patternExportDirectory": "./pattern_exports/"
Coupled with exported css (much easier to extract with existing tools like grunt-contrib-copy), pattern export can help to maintain the relevancy of the design system by directly placing partials in a directory of your choosing.
patternlab.json
is a file created for debugging purposes. Set debug
to true in .config.json
to see all the secrets.
Running grunt serve
will compile the patternlab front end and host it on http://localhost:9001 by default. Page will reload on any saved source code change.
===
The Node version of Pattern Lab is maintained by @bmuenzenmeyer and contributors. Pull requests welcome, but please take a moment to read the guidelines.
You can find some simple upgrade documenation in it's current home here (unreleased but confirmed to work): https://github.com/pattern-lab/website/blob/dev/patternlabsite/docs/node/upgrading.md
Dave Olsen has published the specification for Pattern Lab ports. Development will be oriented toward compliance with this as the spec and the port mature together.
A lot of good conversation has revolved around whether Pattern Lab is a platform or a tool in the toolbox, part of a larger solution. It's my goal to #1) adhere to the specification and #2) meet the needs of both use cases.
If you want to only build the patterns, alter your Gruntfile.js
patternlab task to the following:
grunt.registerTask('default', ['clean', 'concat', 'patternlab:only_patterns', /*'sass',*/ 'copy']);
This will output compiled patterns to ./public/patterns/
The core patternlab engine is free of any dependencies to grunt, allowing users to integrate with gulp if desired. Future efforts here will orient towards this gulp implementation by oscar-g.
===
(The following documentation is built for the PHP version of Pattern Lab, but most applies to the node version too. If you find omissions or mistakes please open an issue.)
Patterns are the core element of Pattern Lab. Understanding how they work is the key to getting the most out of the system. Patterns use Mustache so please read Mustache's docs as well.
- How Patterns Are Organized
- Adding New Patterns
- Reorganizing Patterns
- Including One Pattern Within Another via Partials
- Managing Assets for a Pattern: JavaScript, images, CSS, etc.
- Modifying the Pattern Header and Footer
- Using Pattern Parameters
- Using Pattern State
- "Hiding" Patterns in the Navigation
- Adding Annotations
- Viewing Patterns on a Mobile Device
The Node version of Pattern Lab utilizes Mustache as the template language for patterns. In addition to allowing for the inclusion of one pattern within another it also gives pattern developers the ability to include variables. This means that attributes like image sources can be centralized in one file for easy modification across one or more patterns. The Node version of Pattern Lab uses a JSON file, source/_data/data.json
, to centralize many of these attributes.
- Introduction to JSON & Mustache Variables
- Overriding the Central
data.json
Values with Pattern-specific Values - Linking to Patterns with Pattern Lab's Default
link
Variable - Creating Lists with Pattern Lab's Default
listItems
Variable
By default, the Pattern Lab assets can be manually generated and the Pattern Lab site manually refreshed but who wants to waste time doing that? Here are some ways that Pattern Lab can make your development workflow a little smoother:
- Watching for Changes and Auto-Regenerating Patterns - Node version coming soon
- Auto-Reloading the Browser Window When Changes Are Made - Node version coming soon
- Multi-browser & Multi-device Testing with Page Follow
- Keyboard Shortcuts
- Special Pattern Lab-specific Query String Variables
- Preventing the Cleaning of public/ - Node version coming soon
- Modifying the Pattern Lab Nav - Node version coming soon
- Editing the config.ini Options - Node version coming soon