added a convert_encoding filter
authorFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 09:08:36 +0000 (11:08 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 09:12:06 +0000 (11:12 +0200)
CHANGELOG
doc/filters/convert_encoding.rst [new file with mode: 0644]
lib/Twig/Extension/Core.php

index c7f4562..cf0646f 100644 (file)
--- 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 (file)
index 0000000..569b6bf
--- /dev/null
@@ -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
index 6ca8331..2d2c22e 100644 (file)
@@ -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).');
     }