tc/twig-global-bundle

TcTwigGlobalBundle

v1.0.0 2015-05-07 14:40 UTC

This package is not auto-updated.

Last update: 2024-04-13 15:17:40 UTC


README

Latest Stable Version

SensioLabsInsight

Provides some useful twig tags for global variables.

Installation

composer require tc/twig-global-bundle

Enable the bundle in your AppKernel.php

$bundles = array(
    // ...
    
    new Tc\Bundle\TwigGlobal\TcTwigGlobalBundle(),
    
    // ...
);

Usage

Usage in Twig:

{# set a global #}
{% global title = 'My Title' %}

{# get a global #}
{% global title %}

{# merging #}
{% global title ~ ' | My Title Suffix' %}

{# set defaults #}
{% global title 'My Title' default %}
{# this can be overwritten in a child template #}
{% global title 'My Other Title' %}

{# working with arrays #}
{% global colors = ['red', 'green', 'blue'] %}
{% global colors ~ 'purple' %} {# red, green, blue, purple #}
{% global colors ~ ['red', 'blue', 'yellow'] unique %} {# red, green, blue, purple, yellow #}

{# access globals outside of tag #}
{{ _tc_global.get('title', 'some default value') }}
{{ _tc_global.set('title', 'value', true /* default */) }}
{{ _tc_global.merge('colors', 'purple', false /* default */, false /* unique */) }}

Usage in PHP via the tc.twig_global service:

$container->get('tc.twig_global')->set('title', 'something');
$container->get('tc.twig_global')->get('title');
$container->get('tc.twig_global')->merge('colors', 'blue');
// etc

License

TcTwigGlobalBundle is licensed with the MIT license.

See LICENSE for more details.