Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 688b960

Browse files
committed
Merge branch 'dev'
2 parents 193ec46 + c00436a commit 688b960

File tree

7 files changed

+185
-69
lines changed

7 files changed

+185
-69
lines changed

README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,83 @@
11
# Mustache PatternEngine for Pattern Lab PHP
22

3-
The Mustache PatternEngine allows you to use [Mustache](https://mustache.github.io) as the template language for Pattern Lab PHP. Once the PatternEngine is installed you can use Mustache-based StarterKits and StyleguideKits.
3+
The Mustache PatternEngine allows you to use [Mustache](https://mustache.github.io) as the template language for Pattern Lab PHP. Once the PatternEngine is installed you can use Mustache-based StarterKits and StyleguideKits.
44

55
## Installation
66

7-
Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project dependencies. To install the Mustache PatternEngine:
7+
The Mustache PatternEngine will come pre-installed with the **Pattern Lab Standard Edition**.
8+
9+
### Composer
10+
11+
Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project dependencies with Pattern Lab Editions. To add the Mustache PatternEngine to the dependencies list for your Edition you can type the following in the command line at the base of your project:
812

913
composer require pattern-lab/patternengine-mustache
1014

15+
See Packagist for [information on the latest release](https://packagist.org/packages/pattern-lab/patternengine-mustache).
16+
17+
## Overview
18+
19+
This document is broken into two parts:
20+
21+
* [Extending Mustache](#extending-mustache)
22+
* [Available Loaders for Plugin Developers](#available-loaders)
23+
24+
## Extending Mustache
25+
26+
Mustache comes with two ways to extend the underlying template parser:
27+
28+
* [Filters](https://github.com/bobthecow/mustache.php/wiki/FILTERS-pragma)
29+
* Lambdas
30+
31+
The Mustache PatternEngine enables these features via Helpers.
32+
33+
### Helpers
34+
35+
The requirements for using helpers with Pattern Lab:
36+
37+
* Files must go in `source/_mustache-components/helpers`
38+
* Files must have the extension `.helper.php` (_this can be modified in the config_)
39+
* The helper **must** set the variable `$helper`
40+
* Only one helper per file (_e.g. can only set `$helper` once per file_)
41+
42+
An example function called `verbatim.helper.twig` in `source/_mustache-components/helpers`:
43+
44+
```php
45+
<?php
46+
47+
$helper = function ($text) {
48+
return "{{=%%pl pl%%=}}".$text."%%pl={{ }}=pl%%";
49+
};
50+
51+
?>
52+
```
53+
54+
This helper would be used like this in a pattern. Note that the tag is using the filename and that this is an example of a lambda:
55+
56+
```mustache
57+
{{# verbatim }}
58+
{{ this won't be parsed }}
59+
{{/ verbatim }}
60+
```
61+
62+
Mustache also allows dot notation with helpers. An example function called `case.helper.twig` in `source/_mustache-components/helpers`:
63+
64+
```php
65+
<?php
66+
67+
$helper = array(
68+
'lower' => function($value) { return strtolower((string) $value); },
69+
'upper' => function($value) { return strtoupper((string) $value); },
70+
));
71+
72+
?>
73+
```
74+
75+
This helper would be used like this in a pattern. Note that the tag is using the filename and that this is an example of a filter:
76+
77+
```mustache
78+
{{ greeting | case.upper }}
79+
```
80+
1181
## Available Loaders
1282

1383
If you're building a plugin that will be parsing Mustache files you have access to three loaders. It's recommended that you use these instead of accessing Mustache directly as these loaders will work with other PatternEngines.

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@
3030
},
3131
"extra": {
3232
"patternlab": {
33-
"config": [
34-
{
35-
"lineageMatch": "{{>([ ]+)?([A-Za-z0-9-_]+)(?:\\:[A-Za-z0-9-]+)?(?:(| )\\(.*)?([ ]+)?}}",
36-
"lineageMatchKey": 2,
37-
"patternExtension": "mustache"
38-
}
39-
]
33+
"config": {
34+
"lineageMatch": "{{>([ ]+)?([A-Za-z0-9-_]+)(?:\\:[A-Za-z0-9-]+)?(?:(| )\\(.*)?([ ]+)?}}",
35+
"lineageMatchKey": 2,
36+
"patternExtension": "mustache",
37+
"mustacheHelperExt": "helper.php"
38+
}
4039
}
4140
}
4241
}

src/PatternLab/PatternEngine/Mustache/Helper.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/PatternLab/PatternEngine/Mustache/Loaders/FilesystemLoader.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,35 @@
1313
namespace PatternLab\PatternEngine\Mustache\Loaders;
1414

1515
use \PatternLab\PatternEngine\Loader;
16+
use \PatternLab\PatternEngine\Mustache\MustacheUtil;
1617

1718
class FilesystemLoader extends Loader {
18-
19+
1920
/**
2021
* Load a new Mustache instance that uses the File System Loader
2122
*/
2223
public function __construct($options = array()) {
23-
24+
2425
$mustacheOptions = array();
2526
$mustacheOptions["loader"] = new \Mustache_Loader_FilesystemLoader($options["templatePath"]);
2627
$mustacheOptions["partials_loader"] = new \Mustache_Loader_FilesystemLoader($options["partialsPath"]);
27-
28+
$mustacheOptions["helpers"] = MustacheUtil::loadHelpers();
29+
$mustacheOptions["pragmas"] = array(\Mustache_Engine::PRAGMA_FILTERS);
30+
2831
$this->instance = new \Mustache_Engine($mustacheOptions);
29-
32+
3033
}
31-
34+
3235
/**
3336
* Render a template
3437
* @param {Array} the options to be rendered by Mustache
3538
*
3639
* @return {String} the rendered result
3740
*/
3841
public function render($options = array()) {
39-
42+
4043
return $this->instance->render($options["template"], $options["data"]);
41-
44+
4245
}
43-
46+
4447
}

src/PatternLab/PatternEngine/Mustache/Loaders/PatternLoader.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,39 @@
1616
use \PatternLab\Dispatcher;
1717
use \PatternLab\PatternEngine\Mustache\Loaders\Mustache\PatternStringLoader as Mustache_Loader_PatternStringLoader;
1818
use \PatternLab\PatternEngine\Mustache\Loaders\Mustache\PatternPartialLoader as Mustache_Loader_PatternPartialLoader;
19-
use \PatternLab\PatternEngine\Mustache\Helper;
19+
use \PatternLab\PatternEngine\Mustache\MustacheUtil;
2020
use \PatternLab\PatternEngine\Loader;
2121

2222
class PatternLoader extends Loader {
23-
23+
2424
/**
2525
* Load a new Mustache instance that uses the Pattern Loadere
2626
*/
2727
public function __construct($options = array()) {
28-
29-
Dispatcher::getInstance()->dispatch("mustacheRule.gatherHelpers");
30-
28+
3129
//default var
3230
$patternSourceDir = Config::getOption("patternSourceDir");
33-
31+
3432
$mustacheOptions = array();
3533
$mustacheOptions["loader"] = new Mustache_Loader_PatternStringLoader();
3634
$mustacheOptions["partials_loader"] = new Mustache_Loader_PatternPartialLoader($patternSourceDir,array("patternPaths" => $options["patternPaths"]));
37-
$mustacheOptions["helpers"] = Helper::get();
38-
35+
$mustacheOptions["helpers"] = MustacheUtil::loadHelpers();
36+
$mustacheOptions["pragmas"] = array(\Mustache_Engine::PRAGMA_FILTERS);
37+
3938
$this->instance = new \Mustache_Engine($mustacheOptions);
40-
39+
4140
}
42-
41+
4342
/**
4443
* Render a pattern
4544
* @param {Array} the options to be rendered by Mustache
4645
*
4746
* @return {String} the rendered result
4847
*/
4948
public function render($options = array()) {
50-
49+
5150
return $this->instance->render($options["pattern"], $options["data"]);
52-
51+
5352
}
54-
53+
5554
}

src/PatternLab/PatternEngine/Mustache/Loaders/StringLoader.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313
namespace PatternLab\PatternEngine\Mustache\Loaders;
1414

1515
use \PatternLab\PatternEngine\Loader;
16+
use \PatternLab\PatternEngine\Mustache\MustacheUtil;
1617

1718
class StringLoader extends Loader {
18-
19+
1920
/**
2021
* Load a new Mustache instance that is just a vanilla Mustache rendering engine for strings
2122
*/
2223
public function __construct($options = array()) {
23-
24-
$this->instance = new \Mustache_Engine;
25-
24+
25+
$mustacheOptions = array();
26+
$mustacheOptions["helpers"] = MustacheUtil::loadHelpers();
27+
$mustacheOptions["pragmas"] = array(\Mustache_Engine::PRAGMA_FILTERS);
28+
29+
$this->instance = new \Mustache_Engine($mustacheOptions);
30+
2631
}
27-
32+
2833
/**
2934
* Render a string
3035
* @param {Array} the options to be rendered by Mustache
3136
*
3237
* @return {String} the rendered result
3338
*/
3439
public function render($options = array()) {
35-
40+
3641
return $this->instance->render($options["string"], $options["data"]);
37-
42+
3843
}
39-
44+
4045
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*!
4+
* Mustache Util Class
5+
*
6+
* Copyright (c) 2015 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* Various utility methods for the Mustache PatternEngine
10+
*
11+
*/
12+
13+
namespace PatternLab\PatternEngine\Mustache;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\Console;
17+
use \Symfony\Component\Finder\Finder;
18+
19+
class MustacheUtil {
20+
21+
/**
22+
* Load helpers for the Mustache PatternEngine
23+
*
24+
* @return {Array} an array of helpers
25+
*/
26+
public static function loadHelpers() {
27+
28+
// set-up helper container
29+
$helpers = array();
30+
31+
// load defaults
32+
$helperDir = Config::getOption("sourceDir").DIRECTORY_SEPARATOR."_mustache-components/helpers";
33+
$helperExt = Config::getOption("mustacheHelperExt");
34+
$helperExt = $helperExt ? $helperExt : "helper.php";
35+
36+
if (is_dir($helperDir)) {
37+
38+
// loop through the filter dir...
39+
$finder = new Finder();
40+
$finder->files()->name("*\.".$helperExt)->in($helperDir);
41+
$finder->sortByName();
42+
foreach ($finder as $file) {
43+
44+
// see if the file should be ignored or not
45+
$baseName = $file->getBasename();
46+
if ($baseName[0] != "_") {
47+
48+
include($file->getPathname());
49+
50+
// $key may or may not be defined in the included file
51+
// $helper needs to be defined in the included file
52+
if (isset($helper)) {
53+
if (!isset($key)) {
54+
$key = $file->getBasename(".".$helperExt);
55+
}
56+
$helpers[$key] = $helper;
57+
unset($helper);
58+
unset($key);
59+
}
60+
61+
}
62+
63+
}
64+
65+
}
66+
67+
return $helpers;
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)