Twig Extensions
, (*1)
Add more fun to Twig. Inspired by Jinja2, Jekyll ..., (*2)
``` jinja
{% block css %}
{% endblock %}, (*3)
{% unless user.active %}, (*4)
{% endunless %}, (*5)
{{ gist(6421704) }}, (*6)
{{ post.content|markdown }}, (*7)
{% pygments brainfuck linenos %}
+++++++++[>++++++++>+++++++++++>+++++<<-].>++.+++++++..+++.>-.
------------.++++++++.--------.+++.------.--------.+.
{% endpygments %}, (*8)
Customized filesystem loader
``` php
<?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
Optional requirements
-
Python2.4+ and Pygments (kzykhys/Pygments.php) for SyntaxHighlight extension, (*9)
json
{
"require": {
"kzykhys/pygments": ">=1.0"
}
}
, (*10)
-
PHP5.4+ and kzykhys/Ciconia for Markdown extension, (*11)
json
{
"require": {
"kzykhys/ciconia": ">=1.0"
}
}
, (*12)
-
leafo/lessphp for Less extension, (*13)
json
{
"require": {
"leafo/lessphp": ">=0.4"
}
}
, (*14)
-
symfony/process for Pipe extension, (*15)
json
{
"require": {
"symfony/process": ">=2.3"
}
}
, (*16)
Installation
``` json
{
"require": {
"kzykhys/twig-extensions": "dev-master"
}
}, (*17)
``` php
<?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
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, (*18)
``` php
$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader);
$twig->setExtensions(ExtensionAggregate::getExtensions());, (*19)
Or register extensions which you like
``` php
$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
Filters
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 |
Functions
Tests
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 |
License
The MIT License, (*20)
Author
Kazuyuki Hayashi (@kzykhys), (*21)