Code Monkey home page Code Monkey logo

twigextensionsbundle's Introduction

Information

TwigExtensionsBundle is just a collection of Twig extensions i find useful.

DecorateEmptyValueExtension

Provides an enhanced default filter to decorate empty values with a placeholder which can even be HTML.

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

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

to make it render properly. With this extension you can write

{{ somevalue | craue_default }}

instead.

ArrayHelperExtension

Provides a without filter wrapping PHP's array_diff function.

FormatDateTimeExtension

Provides filters for locale-aware formatting of date, time, and date/time values.

ChangeLanguageExtension

Providing helpers for implementing a language change mechanism and handling localized routes.

Installation

Add TwigExtensionsBundle to your vendor/Craue directory

git submodule add git://github.com/craue/TwigExtensionsBundle.git vendor/Craue/TwigExtensionsBundle

Add TwigExtensionsBundle to your application kernel

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

Register the Craue namespace

// app/autoload.php
$loader->registerNamespaces(array(
	// ...
	'Craue' => __DIR__.'/../vendor',
));

Make the Twig extensions available by updating your configuration

// app/config/config.yml
craue_twig_extensions: ~

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"

FormatDateTimeExtension

<h2>with current locale</h2>
date: {{ someDateTimeValue | craue_date }}<br />
time: {{ someDateTimeValue | craue_time }}<br />
both: {{ someDateTimeValue | craue_datetime }}

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

ChangeLanguageExtension

Rendering links for some kind of "change language" menu:

<ul>
	{% for locale in craue_availableLocales %}
		<li>
			{% if locale == app.session.locale %}
				{{ craue_languageName(locale) }}
			{% else %}
				<a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.all
						| craue_cleanRouteParameters | merge({'_locale': locale})) }}"
					>{{ craue_languageName(locale) }}</a>
			{% endif %}
		</li>
	{% endfor %}
</ul>

Additionally, instead of

<a href="{{ path('route', {'_locale': app.session.locale}) }}">text</a>

you can use

<a href="{{ craue_localizedPath('route') }}">text</a>

to build links containing the current locale.

Set/override default values

DecorateEmptyValueExtension

; app/config/parameters.ini
craue_twig_extensions.decorateEmptyValue.placeholder="&ndash;"

FormatDateTimeExtension

; app/config/parameters.ini
craue_twig_extensions.formatDateTime.datetype="full"
craue_twig_extensions.formatDateTime.timetype="short"

ChangeLanguageExtension

; app/config/parameters.ini
craue_twig_extensions.changeLanguage.availableLocales[]="de"
craue_twig_extensions.changeLanguage.availableLocales[]="en"
craue_twig_extensions.changeLanguage.availableLocales[]="ru"
craue_twig_extensions.changeLanguage.showForeignLanguageNames=true
craue_twig_extensions.changeLanguage.showFirstUppercase=false

With XML for example you can also set the keys to be more specific about the locales:

<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

DecorateEmptyValueExtension

Alias

You can define an alias for the filter if you don't like to write

{{ somevalue | craue_default }}

all the time. Setting this to "d" for example with

; app/config/parameters.ini
craue_twig_extensions.decorateEmptyValue.filterAlias="d"

allows you to write

{{ somevalue | d }}

in your Twig Template. But pay attention to not accidentally override built-in filters, although you can do it intentionally, e.g. by setting the alias to "default".

ArrayHelperExtension

Alias

Similar to the DecorateEmptyValueExtension you can define an alias:

; app/config/parameters.ini
craue_twig_extensions.arrayHelper.withoutAlias="without"

But, again, pay attention to not accidentally override built-in filters, although you can do it intentionally.

FormatDateTimeExtension

Aliases

Again, you can define an alias for each filter:

; app/config/parameters.ini
craue_twig_extensions.formatDateTime.dateFilterAlias="date"
craue_twig_extensions.formatDateTime.timeFilterAlias="time"
craue_twig_extensions.formatDateTime.dateTimeFilterAlias="datetime"

Don't accidentally override built-in filters, although you can do it intentionally, e.g. by setting the dateFilterAlias to "date".

ChangeLanguageExtension

Aliases

Again, you can define aliases:

; app/config/parameters.ini
craue_twig_extensions.changeLanguage.languageNameAlias=
craue_twig_extensions.changeLanguage.localizedPathAlias="path"
craue_twig_extensions.changeLanguage.cleanRouteParametersAlias=
craue_twig_extensions.changeLanguage.availableLocalesAlias=

Don't accidentally override built-in filters/functions/globals, although you can do it intentionally, e.g. by setting the localizedPathAlias to "path".

twigextensionsbundle's People

Contributors

craue avatar p16 avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.