Skip to content
This repository was archived by the owner on Dec 10, 2019. It is now read-only.

Commit 601c143

Browse files
committed
Merge pull request #279 from sghoweri/pattern-engines
v3 Twig Engine Integration
2 parents 0a4f6ee + bbc2c16 commit 601c143

File tree

7 files changed

+140
-4
lines changed

7 files changed

+140
-4
lines changed

Gruntfile.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ module.exports = function (grunt) {
8484
files: [
8585
path.resolve(paths().source.css + '**/*.css'),
8686
path.resolve(paths().source.styleguide + 'css/*.css'),
87-
path.resolve(paths().source.patterns + '**/*.mustache'),
88-
path.resolve(paths().source.patterns + '**/*.json'),
87+
path.resolve(paths().source.patterns + '**/*'),
8988
path.resolve(paths().source.fonts + '/*'),
9089
path.resolve(paths().source.images + '/*'),
9190
path.resolve(paths().source.data + '*.json')
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* handlebars pattern engine for patternlab-node - v0.15.1 - 2015
3+
*
4+
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
5+
* Licensed under the MIT license.
6+
*
7+
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
8+
*
9+
*/
10+
11+
/*
12+
* ENGINE SUPPORT LEVEL:
13+
*
14+
* Full. Partial calls and lineage hunting are supported. Handlebars does not
15+
* support the mustache-specific syntax extensions, style modifiers and pattern
16+
* parameters, because their use cases are addressed by the core Handlebars
17+
* feature set.
18+
*
19+
*/
20+
21+
(function () {
22+
"use strict";
23+
24+
var Twig = require('twig/twig.js');
25+
var twig = Twig.twig;
26+
27+
var engine_twig = {
28+
engine: Twig,
29+
engineName: 'twig',
30+
engineFileExtension: '.twig',
31+
32+
//Important! Needed for Twig compilation. Can't resolve paths otherwise.
33+
expandPartials: true,
34+
35+
// regexes, stored here so they're only compiled once
36+
findPartialsRE: /{%([ ]+)?(?:extends|include|embed)(\s\S)(.*)%}/g,
37+
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g, // TODO
38+
39+
// render it
40+
renderPattern: function renderPattern(template, data) {
41+
var template = twig({
42+
data: template
43+
}).render(data);
44+
45+
return template;
46+
},
47+
48+
// find and return any {{> template-name }} within pattern
49+
findPartials: function findPartials(pattern) {
50+
var matches = pattern.template.match(this.findPartialsRE);
51+
return matches;
52+
},
53+
findPartialsWithStyleModifiers: function() {
54+
// TODO: make the call to this from oPattern objects conditional on their
55+
// being implemented here.
56+
return [];
57+
},
58+
// returns any patterns that match {{> value(foo:"bar") }} or {{>
59+
// value:mod(foo:"bar") }} within the pattern
60+
findPartialsWithPatternParameters: function() {
61+
// TODO: make the call to this from oPattern objects conditional on their
62+
// being implemented here.
63+
return [];
64+
},
65+
findListItems: function(pattern) {
66+
var matches = pattern.template.match(this.findListItemsRE);
67+
return matches;
68+
},
69+
// given a pattern, and a partial string, tease out the "pattern key" and
70+
// return it.
71+
findPartialKey: function(partialString) {
72+
73+
74+
//var partialKey = partialString.replace(this.findPartialsRE, '$1');
75+
var partialKey = partialString.match(/"((?:\\.|[^"\\])*)"/)[0];
76+
partialKey = partialKey.replace(/"/g, '');
77+
78+
return partialKey;
79+
}
80+
81+
82+
83+
84+
};
85+
86+
module.exports = engine_twig;
87+
})();

package.gulp.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"fs-extra": "^0.26.5",
1010
"glob": "^7.0.0",
1111
"html-entities": "^1.2.0",
12-
"mustache": "^2.2.1"
12+
"mustache": "^2.2.1",
13+
"handlebars": "^4.0.5",
14+
"twig": "^0.8.8",
15+
"underscore": "^1.8.3",
1316
},
1417
"devDependencies": {
1518
"browser-sync": "^2.11.1",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"matchdep": "^1.0.0",
1212
"mustache": "^2.2.0",
1313
"handlebars": "^4.0.5",
14-
"underscore": "^1.8.3"
14+
"underscore": "^1.8.3",
15+
"twig": "^0.8.8"
1516
},
1617
"devDependencies": {
1718
"bs-html-injector": "^3.0.0",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<style>
2+
.btn {
3+
padding: 10px;
4+
border-radius: 10px;
5+
display: inline-block;
6+
text-align: center;
7+
}
8+
</style>
9+
10+
<a href="#" class="btn">Button</a>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<img src="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg"
2+
srcset="http://placeholdit.imgix.net/~text?txtsize=33&txt=280%C3%97220&w=280&h=220&fm=pjpg 280w,
3+
http://placeholdit.imgix.net/~text?txtsize=33&txt=560%C3%97440&w=560&h=440&fm=pjpg 560w,
4+
http://placeholdit.imgix.net/~text?txtsize=33&txt=840%C3%97660&w=840&h=660&fm=pjpg 840w"
5+
sizes="100vw">
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<style>
2+
.Media {
3+
display: flex;
4+
align-items: flex-start;
5+
}
6+
7+
.Media > img {
8+
margin-right: 1em;
9+
max-width: 200px;
10+
}
11+
12+
.Media-body {
13+
flex: 1;
14+
}
15+
</style>
16+
17+
18+
{% set foo = "world!" %}
19+
20+
21+
<div class="Media">
22+
{% include "atoms-image" %}
23+
24+
<div class="Media-body">
25+
26+
{# Testing if we can also pull in non-twig templates without breaking anything (it works!). Good idea though? Eh... #}
27+
{% include "atoms-paragraph" %}
28+
29+
<p>Oh, hello {{ foo }}</p>
30+
</div>
31+
</div>

0 commit comments

Comments
 (0)