|
| 1 | +# Twig PatternEngine for Pattern Lab PHP |
| 2 | + |
| 3 | +The Twig PatternEngine allows you to use [Twig](http://twig.sensiolabs.org) as the template language for Pattern Lab PHP. Once the PatternEngine is installed you can use Twig-based StarterKits and StyleguideKits. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project dependencies. To install the Twig PatternEngine: |
| 8 | + |
| 9 | + composer require pattern-lab/patternengine-twig |
| 10 | + |
| 11 | +## Available Loaders |
| 12 | + |
| 13 | +If you're building a plugin that will be parsing Twig files you have access to three loaders. It's recommended that you use these instead of accessing Twig directly as these loaders will work with other PatternEngines. |
| 14 | + |
| 15 | +### The String Loader |
| 16 | + |
| 17 | +The string loader takes a simple string and compiles it. To use: |
| 18 | + |
| 19 | +```php |
| 20 | +$data = array("hello" => "world"); |
| 21 | +$string = "If I say hello you say {{ hello }}."; |
| 22 | +$stringLoader = \PatternLab\Template::getStringLoader(); |
| 23 | +$output = $stringLoader->render(array("string" => $string, "data" => $data)); |
| 24 | +print $output; // outputs "If I say hello you say world." |
| 25 | +``` |
| 26 | + |
| 27 | +### The Filesystem Loader |
| 28 | + |
| 29 | +The filesystem loader will look for templates in the configured StyleguideKit directory and compile them. The template location for the filesystem loader can't be modified. To use: |
| 30 | + |
| 31 | +```php |
| 32 | +$data = array(...); |
| 33 | +$filesystemLoader = \PatternLab\Template::getFilesystemLoader(); |
| 34 | +$output = $filesystemLoader->render(array("template" => "viewall", "data" => $data)); |
| 35 | +print $output; // outputs the viewall view from the configured styleguidekit |
| 36 | +``` |
| 37 | + |
| 38 | +### The Pattern Loader |
| 39 | + |
| 40 | +The pattern loader looks for patterns and allows the use of the Pattern Lab-specific partial syntax. To use: |
| 41 | + |
| 42 | +```php |
| 43 | +$data = array(...); |
| 44 | +$patternPath = "path/to/pattern"; |
| 45 | +$patternEngineBasePath = \PatternLab\PatternEngine::getInstance()->getBasePath(); |
| 46 | +$patternLoaderClass = $patternEngineBasePath."\Loaders\PatternLoader"; |
| 47 | +$patternLoader = new $patternLoaderClass($options); |
| 48 | +$code = $patternLoader->render(array("pattern" => $patternPath, "data" => $data)); |
| 49 | +print $output; // outputs the given pattern |
0 commit comments