Extensions for Twig

Installs: 1 227

Dependents: 0

Stars: 6

Watchers: 2

Forks: 1

v0.1.0 2013-12-20 10:08 UTC


Latest Unstable Version Build Status Coverage Status SensioLabsInsight

Add more fun to Twig. Inspired by Jinja2, Jekyll ...

{% block css %}
  <style type="text/css">
  {% less %}
    {% include 'mixins.less' %}
    .container {
      .row {
        &:first-child { margin-bottom: @m; }
  {% endless %}
{% endblock %}

{% unless %}
  <div class="alert">Please <a href="#">Login</a></div>
{% endunless %}

{{ gist(6421704) }}

{{ post.content|markdown }}

{% pygments brainfuck linenos %}
{% endpygments %}

Customized filesystem loader


use KzykHys\TwigExtensions\Loader\Filesystem;

$loader = new Filesystem('/path/to/templates');
$twig   = new Twig_Environment($loader);

$twig->render(array('date.twig', 'archive.twig', 'default.twig'));


  • PHP5.3

Optional requirements

  • Python2.4+ and Pygments (kzykhys/Pygments.php) for SyntaxHighlight extension

        "require": {
            "kzykhys/pygments": ">=1.0"
  • PHP5.4+ and kzykhys/Ciconia for Markdown extension

        "require": {
            "kzykhys/ciconia": ">=1.0"
  • leafo/lessphp for Less extension

        "require": {
            "leafo/lessphp": ">=0.4"
  • symfony/process for Pipe extension

        "require": {
            "symfony/process": ">=2.3"


    "require": {
        "kzykhys/twig-extensions": "dev-master"

use KzykHys\TwigExtensions\ExtensionAggregate;

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig   = new Twig_Environment($loader);

// Registers all extensions

A List of Extensions

Extension Description
Core Provides tags and tests
Text Utilities for strings
Snippet Embedding various snippets
Markdown Github Flavored Markdown support
SyntaxHighlight Syntax Highlighting (pygments/google-code-prettify)
Less Compiles LESS to CSS
Pipe Use external commands as filters

Registering extensions to Twig

Register all-in-one extension

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig   = new Twig_Environment($loader);

Or register extensions which you like

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig   = new Twig_Environment($loader);
$twig->addExtension(new \KzykHys\TwigExtensions\Extension\Core());
$twig->addExtension(new \KzykHys\TwigExtensions\Extension\Snippet());

Twig Extensions Reference


Name Extension Description
unless Core Negated form of the if statement
pygments SyntaxHighlight Highlight the code (requires Pygments.php)
less Less Converts LESS to CSS


Name Extension Description
center Text Centers the value in a field of a given width.
filesizeformat Text Format the value like a ‘human-readable’ file size (i.e. 13 kB, 4.1 MB, 102 Bytes, etc)
markdown Markdown Transform markdown into html
md5 Text Calculate the md5 hash of a string
sha1 Text Calculate the sha1 hash of a string
sha512 Text Calculate the sha512 hash of a string
pipe Pipe Executes an external command


Name Extension Description
call Core Call a callback
lipsum Text Generates some lorem ipsum for the template
gist Snippet Embed a gist
pastebin Snippet Embed a pastebin


Name Extension Description
callable Core Return whether the object is callable
instanceof Core Return whether a variable is an instantiated object of a certain class


The MIT License


Kazuyuki Hayashi (@kzykhys)