kzykhys/twig-extensions

Extensions for Twig

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

This package is not auto-updated.

Last update: 2024-12-17 03:31:00 UTC


README

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 %}
  </style>
{% endblock %}

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

{{ gist(6421704) }}

{{ post.content|markdown }}

{% pygments brainfuck linenos %}
+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.
------------.<++++++++.--------.+++.------.--------.>+.
{% endpygments %}

Customized filesystem loader

<?php

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'));

Requirements

  • 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"
        }
    }

Installation

{
    "require": {
        "kzykhys/twig-extensions": "dev-master"
    }
}
<?php

use KzykHys\TwigExtensions\ExtensionAggregate;

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

// Registers all extensions
$twig->setExtensions(ExtensionAggregate::getExtensions());

A List of Extensions

Registering extensions to Twig

Register all-in-one extension

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig   = new Twig_Environment($loader);
$twig->setExtensions(ExtensionAggregate::getExtensions());

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

Tags

Filters

Functions

Tests

License

The MIT License

Author

Kazuyuki Hayashi (@kzykhys)