From c015e8184a5e4e6c8b953b207364a51a0475c80e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 16 Oct 2011 11:08:36 +0200 Subject: [PATCH] added a convert_encoding filter --- CHANGELOG | 1 + doc/filters/convert_encoding.rst | 21 +++++++++++++++++++++ lib/Twig/Extension/Core.php | 15 ++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 doc/filters/convert_encoding.rst diff --git a/CHANGELOG b/CHANGELOG index c7f4562..cf0646f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0 + * added a convert_encoding filter * moved all node manipulations outside the compile() Node method * made several speed optimizations diff --git a/doc/filters/convert_encoding.rst b/doc/filters/convert_encoding.rst new file mode 100644 index 0000000..569b6bf --- /dev/null +++ b/doc/filters/convert_encoding.rst @@ -0,0 +1,21 @@ +``convert_encoding`` +==================== + +.. versionadded:: 1.4 + The ``convert_encoding`` filter was added in Twig 1.4. + +The ``convert_encoding`` filter converts a string from one encoding to +another. The first argument is the expected output charset and the second one +is the input charset: + +.. code-block:: jinja + + {{ data|convert_encoding('UTF-8', 'iso-2022-jp') }} + +.. note:: + + This filter relies on the `iconv`_ or `mbstring`_ extension. So one of + them must be installed. + +.. _`iconv`: http://php.net/iconv +.. _`mbstring`: http://php.net/mbstring diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 6ca8331..2d2c22e 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -51,8 +51,9 @@ class Twig_Extension_Core extends Twig_Extension 'replace' => new Twig_Filter_Function('twig_strtr'), // encoding - 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'), - 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'), + 'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'), + 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'), + 'convert_encoding' => new Twig_Filter_Function('twig_convert_encoding'), // string filters 'title' => new Twig_Filter_Function('twig_title_string_filter', array('needs_environment' => true)), @@ -495,7 +496,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $cha // escape all non-alphanumeric characters // into their \xHH or \uHHHH representations if ('UTF-8' != $charset) { - $string = _twig_convert_encoding($string, 'UTF-8', $charset); + $string = twig_convert_encoding($string, 'UTF-8', $charset); } if (null === $string = preg_replace_callback('#[^\p{L}\p{N} ]#u', '_twig_escape_js_callback', $string)) { @@ -503,7 +504,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $cha } if ('UTF-8' != $charset) { - $string = _twig_convert_encoding($string, $charset, 'UTF-8'); + $string = twig_convert_encoding($string, $charset, 'UTF-8'); } return $string; @@ -533,17 +534,17 @@ function twig_escape_filter_is_safe(Twig_Node $filterArgs) } if (function_exists('iconv')) { - function _twig_convert_encoding($string, $to, $from) + function twig_convert_encoding($string, $to, $from) { return iconv($from, $to, $string); } } elseif (function_exists('mb_convert_encoding')) { - function _twig_convert_encoding($string, $to, $from) + function twig_convert_encoding($string, $to, $from) { return mb_convert_encoding($string, $to, $from); } } else { - function _twig_convert_encoding($string, $to, $from) + function twig_convert_encoding($string, $to, $from) { throw new Twig_Error_Runtime('No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).'); } -- 1.7.2.5