PHPackages                             gajdamaka/patternengine-twig - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. gajdamaka/patternengine-twig

ActivePatternlab-patternengine[Templating &amp; Views](/categories/templating)

gajdamaka/patternengine-twig
============================

Twig-based PatternEngine for Pattern Lab.

2.2.4(4y ago)019MITPHP

Since Mar 2Pushed 4y agoCompare

[ Source](https://github.com/gajdamaka/patternengine-php-twig)[ Packagist](https://packagist.org/packages/gajdamaka/patternengine-twig)[ Docs](http://patternlab.io)[ RSS](/packages/gajdamaka-patternengine-twig/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (35)Used By (0)

Twig PatternEngine for Pattern Lab
==================================

[](#twig-patternengine-for-pattern-lab)

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.

Installation
------------

[](#installation)

The Twig PatternEngine comes pre-installed with the [Pattern Lab Standard Edition for Twig](https://github.com/pattern-lab/edition-php-twig-standard). Please start there for all your Twig needs.

### Composer

[](#composer)

Pattern Lab PHP uses [Composer](https://getcomposer.org/) to manage project dependencies with Pattern Lab Editions. To add the Twig PatternEngine to the dependencies list for your Edition you can type the following in the command line at the base of your project:

```
composer require pattern-lab/patternengine-twig

```

See Packagist for [information on the latest release](https://packagist.org/packages/pattern-lab/patternengine-twig).

Overview
--------

[](#overview)

This document is broken into three parts:

- [Working with Patterns and Twig](#working-with-patterns-and-twig)
- [Extending Twig Further](#extending-twig-further)
- [Available Loaders for Plugin Developers](#available-loaders)

Working with Patterns and Twig
------------------------------

[](#working-with-patterns-and-twig)

Twig provides access to two features that may help you extend your patterns, [macros](http://twig.sensiolabs.org/doc/templates.html#macros) and layouts via[template inheritance](http://twig.sensiolabs.org/doc/templates.html#template-inheritance). The Twig PatternEngine also supports the [pattern partial syntax](http://patternlab.io/docs/pattern-including.html) to make including one pattern within another very easy.

- [Pattern includes](#pattern-includes)
- [Macros](#macros)
- [Template inheritance](#template-inheritance)

### Pattern includes

[](#pattern-includes)

Pattern includes take advantage of the [pattern partial syntax](http://patternlab.io/docs/pattern-including.html) as a shorthand for referencing patterns from across the system without needing to rely on absolute paths. The format:

```
{% include "[patternType]-[patternName]" %}

```

For example, let's say we wanted to include the following pattern in a molecule:

```
source/_patterns/00-atoms/03-images/02-landscape-16x9.twig

```

The **pattern type** is *atoms* (from `00-atoms`) and the **pattern name** is *landscape-16x9* from (from `02-landscape-16x9.twig`). Pattern sub-types are never used in this format and any digits for re-ordering are dropped. The shorthand partial syntax for this pattern would be:

```
{% include "atoms-landscape-16x9" %}

```

### Macros

[](#macros)

The requirements for using macros with Pattern Lab:

- Files must go in `source/_macros`
- Files must have the extension `.macro.twig` (*this can be modified in the config*)
- The filename will be used as the base variable name in Twig templates

**Please note:** ensure that there is no overlap between the keys for your macros and the keys for your data attributes. A macro with the name `forms.macro.twig` will conflict with a root key with the name `forms` in your JSON/YAML. Both are accessed via `{{ forms }}` in Twig.

An example of a simple macro called `forms.macro.twig` in `source/_macros`:

```
{% macro input(name) %}
     {{ name }}
{% endmacro %}
```

Would be used like this in a pattern:

```
{{ forms.input("First name") }}
```

### Template inheritance

[](#template-inheritance)

How to use [Template Inheritance](http://twig.sensiolabs.org/doc/templates.html#template-inheritance) with Pattern Lab:

- Files must have the extension `.twig`.
- Files can be extended either by using Pattern Lab's normal shorthand syntax (e.g, `{% extends 'templates-extended-layout'%}`).
- Files can optionally go in `source/_layouts` in order to hide them from the list of patterns and then you can just use the filename as reference (e.g., `{% extends 'extended-layout'%}`).
- Files that are in the same directory can also just use the file name without the shorthand syntax (however, it must include the extension). So if `file1.twig` and `file2.twig` were in same directory, you could place this code in `file2.twig`: `{% extends 'file1.twig' %}`.

An example of a simple layout called `base.twig` in `source/_layouts`:

```

        {% block head %}

            {% block title %}{% endblock %} - My Webpage
        {% endblock %}

        {% block content %}{% endblock %}

            {% block footer %}
                &copy; Copyright 2011 by you.
            {% endblock %}

```

Would be used like this in a pattern:

```
{% extends "base.twig" %}

{% block title %}Index{% endblock %}
{% block head %}
    {{ parent() }}

        .important { color: #336699; }

{% endblock %}
{% block content %}
    Index

        Welcome on my awesome homepage.

{% endblock %}
```

All uses of `extends` above also work with `includes`, `embed` and most likely many other Twig Tags. Let us know if you run into interesting or unexpected use cases!

Extending Twig Further
----------------------

[](#extending-twig-further)

Twig comes with a number of ways to extend the underlying template parser. You can you can add [extra tags](http://twig.sensiolabs.org/doc/advanced.html#tags), [filters](http://twig.sensiolabs.org/doc/advanced.html#filters), [tests](http://twig.sensiolabs.org/doc/advanced.html#tests), and [functions](http://twig.sensiolabs.org/doc/advanced.html#functions). The Twig PatternEngine tries to simplify these extensions by allowing you to create files in specific folders and then auto-load the extensions for you. Learn more about:

- [Filters](#filters)
- [Functions](#functions)
- [Tags](#tags)
- [Tests](#tests)

You can also:

- [Enable `dump()`](#enable-dump)
- [Modify the Default Date and Interval Formats](#modify-the-default-date-and-interval-formats)
- [Quickly disable extensions](#quickly-disable-extensions)

### Filters

[](#filters)

The requirements for using filters with Pattern Lab:

- Files must go in `source/_twig-components/filters`
- Files must have the extension `.filter.php` (*this can be modified in the config*)
- The filter **must** set the variable `$filter`
- Only one filter per file (*e.g. can only set `$filter` once per file*)

An example function called `rot13.filter.php` in `source/_twig-components/filters`:

```

```

This filter would be used like this in a pattern:

```
{{ bar|rot13 }}
```

### Functions

[](#functions)

The requirements for using functions with Pattern Lab:

- Files must go in `source/_twig-components/functions`
- Files must have the extension `.function.php` (*this can be modified in the config*)
- The function **must** set the variable `$function`
- Only one function per file (*e.g. can only set `$function` once per file*)

An example function called `boo.function.php` in `source/_twig-components/functions`:

```

```

This function would be used like this in a pattern:

```
{{ boo("ghost says what?") }}
```

### Tests

[](#tests)

The requirements for using tests with Pattern Lab:

- Files must go in `source/_twig-components/tests`
- Files must have the extension `.test.php` (*this can be modified in the config*)
- The test **must** set the variable `$test`
- Only one test per file (*e.g. can only set `$test` once per file*)

An example of a simple test called `red.test.php` in `source/_twig-components/tests`:

```
