Search code examples
phptwigtemplate-engine

How to install twig localizeddate filter?


I have PHP 5.5.9 and I activated php_intl.dll exyention and set its configuration in the active php.ini as the following:

[intl]
intl.default_locale = ar
; This directive allows you to produce PHP errors when some error
; happens within intl functions. The value is the level of the error produced.
; Default is 0, which does not produce any errors.
;intl.error_level = E_WARNING

When I try the following twig code:

{{ item.get_date('j F Y | g:i a')|localizeddate('medium', 'none', locale) }}

I got a fatal error:

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'The filter "localizeddate" does not exist in "index.html" at line 53' in

The official documentation there does not show how to install or add this filter to twig. I use simple PHP application without Symfony. The twig version 1.16.0


Solution

  • You're looking for the twig/extensions composer package, the github repo is at fabpot/Twig-extensions.

    You mention you're using twig standalone, then you must have a Twig_Environment object. It has an addExtension method, you need to call it, passing a new instance of the Intl extension:

    $env->addExtension(new Twig_Extensions_Extension_Intl());