PHPackages                             camspiers/silverstripe-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. camspiers/silverstripe-twig

ActiveSilverstripe-module[Templating &amp; Views](/categories/templating)

camspiers/silverstripe-twig
===========================

Allows use of twig in SilverStripe 3.1

0.0.4(10y ago)212239[4 PRs](https://github.com/camspiers/silverstripe-twig/pulls)MITPHPPHP &gt;=5.3.0

Since Dec 9Pushed 10y ago6 watchersCompare

[ Source](https://github.com/camspiers/silverstripe-twig)[ Packagist](https://packagist.org/packages/camspiers/silverstripe-twig)[ RSS](/packages/camspiers-silverstripe-twig/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (5)Used By (0)

\#Twig templates for SilverStripe 3.1

\##Overview

SilverStripe Twig enables the use of the Twig templating engine in SilverStripe 3.1.

If you are not familiar with Twig, check out the [docs](http://twig.sensiolabs.org/).

\##Installation

\###Composer

Create or edit a `composer.json` file in the root of your SilverStripe project, and make sure the following is present.

```
{
    "require": {
        "camspiers/silverstripe-twig": "0.0.*",
		"camspiers/autoloader-composer-silverstripe": "1.0.*"
    }
}
```

After completing this step, navigate in Terminal or similar to the SilverStripe root directory and run `composer install` or `composer update` depending on whether or not you have composer already in use.

\##Getting started

\###What to name and where to put your templates

Create a folder called `twig` in your current theme. This is where twig will look for your templates. By default Twig expects your templates to be named with the `.twig` extension, but can be easily configured to look for others (see `twig.extensions`).

The way SilverStripe twig decides which template to use is the same way SilverStripe selects `.ss` templates.

It builds a ranked list of candidate templates based on the class name of the current controller or dataRecord and the action being called. Using the template list it selects the first template that it finds.

For example, for page of PageType `Page`. If there is a `Page.twig` template in the twig folder it will use that.

\###How to enable twig

Twig rendering is enabled by extending the functionality of your SilverStripe controller. This can be done in two ways depending on what version of PHP you have.

\####PHP 5.3

If you want to use twig for all controllers that extend `Page_Controller`, set up is as follows:

`Page.php`

```
class Page_Controller extends TwigContentController
{
}
```

If you want to use twig in a `Controller`, set up is as follows:

`MyController.php`

```
class MyController extends TwigController
{
}
```

\####PHP 5.4

The PHP 5.3 classes above are actually auto-generated from a trait. To use the trait add a `use` statement in your controller as follows:

```
class Page_Controller extends ContentController
{
	use TwigControllerTrait;
}
```

or:

```
class MyController extends Controller
{
	use TwigControllerTrait;
}
```

\###Accessing your Controller in twig

By default twig makes your controller (and therefore your dataRecord) available in your template by the variable `c`.

```
{% for Page in c.Pages %}
	{{ Page.Title }}
{% endfor %}
```

```
{{ c.Title }}
```

```

{% for Page in c.Menu(1) %}
	{{ Page.Title }}
{% else %}
	No pages
{% endfor %}

```

\###Practical usage example

Achieving similar functionality to SilverStripe's `$Layout` variable is easy with twig.

Twig has the concepts of `extends` and `blocks` which enable flexible template reuse.

`Page.twig`

```
{% extends "layouts/layout.twig" %}

{% block head %}
	{{ parent() }}
	{# add some extra assets here #}
{% endblock %}

{% block header %}
	{# add some header content here #}
{% endblock %}

{% block content %}
	{{ c.Title }}
{% endblock %}
```

`layouts/layout.twig`

```

		{% block head %}
			{# default assets here #}
		{% endblock %}

		{% block header %}{% endblock %}
		{% block content %}{% endblock %}

```

\###Configuration

SilverStripe Twig uses a dependency injection container (an extension of `Pimple`) to allow configuration and DI for all objects used.

**Options**

- twig.environment\_options
- twig.extensions
- twig.compilation\_cache
- twig.template\_paths
- twig.controller\_variable\_name

An example:

`mysite/_config.php`

```
TwigContainer::extendConfig(array(
	'twig.environment_options' => array(
        'debug' => true
    ),
    'twig.extensions' => array(
    	'.twig',
    	'.html'
    ),
    'twig.compilation_cache' => BASE_PATH . '/silverstripe-cache',
    'twig.template_paths' => array(
    	THEMES_PATH . '/my-theme/templates'
    ),
    'twig.controller_variable_name' => 'controller'
));
```

Any service provided by SilverStripe Twig can be accessed by instantiating the Container.

```
$dic = new TwigContainer;
$dic['twig']->loadTemplate('template.twig')->render();
```

See [Pimple](http://pimple.sensiolabs.org/) for more information.

\##Using Twig and Haml together SilverStripe twig supports the use of haml through the [SilverStripe haml](https://github.com/camspiers/silverstripe-haml) module.

Install the SilverStripe haml module and you are ready to go.

You can now name your files `.haml` (though you don't have to).

\###Usage

To get Twig to process your file as `Haml` add:

```
{% haml %}
```

To the top of any template you want to be processed as haml.

Example:

```
{% haml %}
!!!
%html
	%head
		%title #{ c.Title } | haml and twig
		- block head
			:javascript
				console.log('yay');
	%body
		- block content
			%h1 #{ c.Title }
			%p #{ c.Content|raw }
			%span.created #{ c.Created|date("d/m/Y") }
```

\##Contributing

\###Code guidelines

This project follows the standards defined in:

- [PSR-1](https://github.com/pmjones/fig-standards/blob/psr-1-style-guide/proposed/PSR-1-basic.md)
- [PSR-2](https://github.com/pmjones/fig-standards/blob/psr-1-style-guide/proposed/PSR-2-advanced.md)

---

\##License

SilverStripe Twig is released under the [MIT license](http://camspiers.mit-license.org/)

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 88.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~348 days

Total

4

Last Release

3857d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/91d02ba78b01d0432821f6195f11d6b95aa8da31f02f8aaea7a2e7215662061a?d=identicon)[camspiers](/maintainers/camspiers)

---

Top Contributors

[![camspiers](https://avatars.githubusercontent.com/u/51294?v=4)](https://github.com/camspiers "camspiers (8 commits)")[![chris-hall-hu](https://avatars.githubusercontent.com/u/3932926?v=4)](https://github.com/chris-hall-hu "chris-hall-hu (1 commits)")

### Embed Badge

![Health badge](/badges/camspiers-silverstripe-twig/health.svg)

```
[![Health](https://phpackages.com/badges/camspiers-silverstripe-twig/health.svg)](https://phpackages.com/packages/camspiers-silverstripe-twig)
```

###  Alternatives

[symfony/twig-bridge

Provides integration for Twig with various Symfony components

2.5k215.5M600](/packages/symfony-twig-bridge)[craftcms/cms

Craft CMS

3.6k3.6M2.6k](/packages/craftcms-cms)[twig/intl-extra

A Twig extension for Intl

36663.2M221](/packages/twig-intl-extra)[twig/string-extra

A Twig extension for Symfony String

21946.0M133](/packages/twig-string-extra)[symfony/ux-twig-component

Twig components for Symfony

21814.8M162](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1635.6M72](/packages/symfony-ux-live-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
