// encoding
'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),
- 'json_encode' => new Twig_Filter_Function('json_encode'),
+ 'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'),
// string filters
'title' => new Twig_Filter_Function('twig_title_string_filter', array('needs_environment' => true)),
return urlencode($url);
}
+function twig_jsonencode_filter($value, $options = 0)
+{
+ if ($value instanceof Twig_Markup) {
+ $value = (string) $value;
+ } elseif (is_array($value)) {
+ array_walk_recursive($value, '_twig_markup2string');
+ }
+
+ return json_encode($value, $options);
+}
+
+function _twig_markup2string(&$value)
+{
+ if ($value instanceof Twig_Markup) {
+ $value = (string) $value;
+ }
+}
+
function twig_array_merge($arr1, $arr2)
{
if (!is_array($arr1) || !is_array($arr2)) {
--- /dev/null
+--TEST--
+"json_encode" filter
+--TEMPLATE--
+{{ "foo"|json_encode|raw }}
+{{ foo|json_encode|raw }}
+{{ [foo, "foo"]|json_encode|raw }}
+--DATA--
+return array('foo' => new Twig_Markup('foo'))
+--EXPECT--
+"foo"
+"foo"
+["foo","foo"]