2017 © Pedro Peláez
 

symfony-bundle twigextensions-bundle

Useful Twig extensions for your Symfony project.

image

craue/twigextensions-bundle

Useful Twig extensions for your Symfony project.

  • Friday, January 5, 2018
  • by craue
  • Repository
  • 5 Watchers
  • 74 Stars
  • 62,705 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 15 Forks
  • 1 Open issues
  • 13 Versions
  • 4 % Grown

The README.md

Information

Build Status, (*1)

TwigExtensionsBundle is a collection of useful Twig extensions for your Symfony project., (*2)

A live demo with code examples can is available at http://craue.de/symfony-playground/en/CraueTwigExtensions/., (*3)

DecorateEmptyValueExtension

Provides an enhanced default filter, craue_default, to decorate empty values with a placeholder which can even be HTML., (*4)

Usually, if you want to use HTML, e.g. the HTML entity —, as value for the default filter in an HTML Twig template you have to do cumbersome, (*5)

{{ somevalue | e | default('—') | raw }}

to make it render properly. With this extension you can write, (*6)

{{ somevalue | craue_default }}

instead., (*7)

ArrayHelperExtension

Provides the filters, (*8)

  • craue_without wrapping PHP's array_diff function,
  • craue_replaceKey which adds/replaces an array entry (whereupon the key can be a variable),
  • craue_removeKey which removes an array entry by key (whereupon the key can be a variable), and
  • craue_translateArray which translates all entries in an array.

FormExtension

Provides a mechanism to render a form several times on one page. This is done by cloning the form prior to rendering using the craue_cloneForm function., (*9)

StringHelperExtension

Provides the craue_trailingDot filter for ensuring that a text ends with a dot. This comes in handy when using error messages (e.g. for validation) of vendor bundles (which are written like sentences but are missing the trailing dots) together with your own ones (which should include the trailing dot)., (*10)

FormatDateTimeExtension

Provides the filters craue_date, craue_time, and craue_datetime for locale-aware formatting of date, time, and date/time values., (*11)

FormatNumberExtension

Provides the filters craue_number, craue_currency, and craue_spellout for locale-aware formatting of numbers and currencies., (*12)

ChangeLanguageExtension

Provides the functions craue_languageName and craue_availableLocales as well as a template for implementing a language change mechanism., (*13)

Installation

Get the bundle

Let Composer download and install the bundle by running, (*14)

composer require craue/twigextensions-bundle

in a shell., (*15)

Enable the bundle

If you don't use Symfony Flex, register the bundle manually:, (*16)

// in config/bundles.php
return [
    // ...
    Craue\TwigExtensionsBundle\CraueTwigExtensionsBundle::class => ['all' => true],
];

Or, for Symfony 3.4:, (*17)

// in app/AppKernel.php
public function registerBundles() {
    $bundles = [
        // ...
        new Craue\TwigExtensionsBundle\CraueTwigExtensionsBundle(),
    ];
    // ...
}

Examples to use the extensions in your Twig template

DecorateEmptyValueExtension

{{ someValueWhichMayBeEmpty | craue_default }}<br />
{{ someValueWhichMayBeEmpty | craue_default('no value') }}<br />
{{ someValueWhichMayBeEmpty | craue_default('&ndash;') }}<br />
{{ someValueWhichMayBeEmpty | craue_default(0) }}

ArrayHelperExtension

{{ anArray | craue_without(aValueOrAnArray) | join(', ') }}<br />
{{ ['red', 'green', 'yellow', 'blue'] | craue_without('yellow') | join(', ') }} will print "red, green, blue"<br />
{{ ['red', 'green', 'yellow', 'blue'] | craue_without(['yellow', 'black', 'red']) | join(', ') }} will print "green, blue"

{{ anArray | craue_replaceKey(key, value) | join(', ') }}<br />
{% set newKey = 'key3' %}
{{ {'key1': 'value1', 'key2': 'value2'} | craue_replaceKey(newKey, 'value3') | join(', ') }} will print "value1, value2, value3"

{{ anArray | craue_removeKey(key) | join(', ') }}<br />
{{ {'key1': 'value1', 'key2': 'value2'} | craue_removeKey('key1') | join(', ') }} will print "value2"

{{ anArray | craue_translateArray() | join(', ') }}<br />

FormExtension

{% for myEntity in myEntities %}
    {% set myFormInstance = craue_cloneForm(myForm) %}
    <form action="{{ path('my_route', {'id': myEntity.getId()}) }}" method="post" {{ form_enctype(myFormInstance) }}>
        {{ form_widget(myFormInstance) }}
        <input type="submit" />
    </form>
{% endfor %}

StringHelperExtension

{{ aString | craue_trailingDot }}<br />
{{ 'This text should end with a dot' | craue_trailingDot }}<br />
{{ 'This text should end with exactly one dot.' | craue_trailingDot }}

FormatDateTimeExtension



with the current locale

date: {{ someDateTimeValue | craue_date }}<br /> time: {{ someDateTimeValue | craue_time }}<br /> both: {{ someDateTimeValue | craue_datetime }}

with a specific locale

date: {{ someDateTimeValue | craue_date('de-DE') }}<br /> time: {{ someDateTimeValue | craue_time('de') }}<br /> both: {{ someDateTimeValue | craue_datetime('en-GB') }}

FormatNumberExtension



with the current locale

thousands separator: {{ someNumber | craue_number }}<br /> default currency: {{ someNumber | craue_currency }}<br /> specific currency: {{ someNumber | craue_currency('EUR') }}<br /> spelled out number: {{ someNumber | craue_spellout }}

with a specific locale

thousands separator: {{ someNumber | craue_number('de-DE') }}<br /> default currency: {{ someNumber | craue_currency(null, 'de-DE') }}<br /> specific currency: {{ someNumber | craue_currency('EUR', 'de-DE') }}<br /> spelled out number: {{ someNumber | craue_spellout('de-DE') }}

ChangeLanguageExtension

There's a Twig template provided which you can use to render a "change language" menu like this:, (*18)

{% include '@CraueTwigExtensions/ChangeLanguage/changeLanguage.html.twig' %}

This will render a list of links to the current route in all defined languages. Wrap it in a div to style it via CSS. Take a look at the template if you want to customize it., (*19)

Set/override default values

DecorateEmptyValueExtension

# in app/config/parameters.yml
  craue_twig_extensions.decorateEmptyValue.placeholder: &ndash;

FormatDateTimeExtension

# in app/config/parameters.yml
  craue_twig_extensions.formatDateTime.datetype: full
  craue_twig_extensions.formatDateTime.timetype: short
  craue_twig_extensions.formatDateTime.timeZone: Europe/Berlin

FormatNumberExtension

# in app/config/parameters.yml
  craue_twig_extensions.formatNumber.currency: EUR

ChangeLanguageExtension

# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.availableLocales: [de, en, ru]
  craue_twig_extensions.changeLanguage.showForeignLanguageNames: true
  craue_twig_extensions.changeLanguage.showFirstUppercase: false

You can also set the keys to be more specific about the locales:, (*20)

# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.availableLocales:
    de_DE: de
    en: en
    ru: ru
<!-- in app/config/parameters.xml -->
<parameter key="craue_twig_extensions.changeLanguage.availableLocales" type="collection">
    <parameter key="de_DE">de</parameter>
    <parameter key="en">en</parameter>
    <parameter key="ru">ru</parameter>
</parameter>

Advanced stuff

Aliases

Optionally, you can define aliases for all provided filters/functions to be used within your project. This allows you to use names you prefer instead of the pre-defined ones. E.g., if you don't like to write, (*21)

{{ somevalue | craue_default }}

all the time, you may define an alias like d for the craue_default filter which allows you to write, (*22)

{{ somevalue | d }}

in your Twig templates. But pay attention to not accidentally override built-in filters/functions, although you can do it intentionally., (*23)

DecorateEmptyValueExtension

# in app/config/parameters.yml
  craue_twig_extensions.decorateEmptyValue.filterAlias: d

ArrayHelperExtension

# in app/config/parameters.yml
  craue_twig_extensions.arrayHelper.withoutAlias: without
  craue_twig_extensions.arrayHelper.replaceKeyAlias: replaceKey
  craue_twig_extensions.arrayHelper.removeKeyAlias: removeKey
  craue_twig_extensions.arrayHelper.translateArrayAlias: translateArray

FormExtension

# in app/config/parameters.yml
  craue_twig_extensions.form.cloneFormAlias: cloneForm

StringHelperExtension

# in app/config/parameters.yml
  craue_twig_extensions.stringHelper.trailingDotAlias: trailingDot

FormatDateTimeExtension

# in app/config/parameters.yml
  craue_twig_extensions.formatDateTime.dateFilterAlias: date
  craue_twig_extensions.formatDateTime.timeFilterAlias: time
  craue_twig_extensions.formatDateTime.dateTimeFilterAlias: datetime

FormatNumberExtension

# in app/config/parameters.yml
  craue_twig_extensions.formatNumber.numberFilterAlias: number
  craue_twig_extensions.formatNumber.currencyFilterAlias: currency
  craue_twig_extensions.formatNumber.spelloutFilterAlias: spellout

ChangeLanguageExtension

# in app/config/parameters.yml
  craue_twig_extensions.changeLanguage.languageNameAlias: languageName
  craue_twig_extensions.changeLanguage.availableLocalesAlias: availableLocales

Enabling only specific extensions

By default, all provided extensions are enabled. If you're using only one or some of them, you may want to disable the others. The following enables them all, so remove the ones you don't need:, (*24)

# in app/config/config.yml
craue_twig_extensions:
  enable_only:
    - ArrayHelperExtension
    - ChangeLanguageExtension
    - DecorateEmptyValueExtension
    - FormatDateTimeExtension
    - FormatNumberExtension
    - FormExtension
    - StringHelperExtension

The Versions

05/01 2018

dev-master

9999999-dev https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3 symfony4

05/01 2018

2.3.1

2.3.1.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3 symfony4

05/01 2018

dev-test

dev-test https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3 symfony4

14/12 2017

2.3.0

2.3.0.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3 symfony4

09/01 2017

2.2.0

2.2.0.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3

29/12 2015

2.1.1

2.1.1.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2 symfony3

01/12 2015

2.1.0

2.1.0.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

20/11 2015

dev-deprecate-twig-global

dev-deprecate-twig-global https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

01/06 2014

2.0.1

2.0.1.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

01/06 2014

2.0.0

2.0.0.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

03/12 2013

1.0.2

1.0.2.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

25/09 2013

1.0.1

1.0.1.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2

28/02 2013

1.0.0

1.0.0.0 https://github.com/craue/TwigExtensionsBundle

Useful Twig extensions for your Symfony2 project.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Christian Raue

twig extensions symfony2