Skip to content

Commit

Permalink
Let PHP convert Twig_Markup to JSON instead of pre-filtering it
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Jan 7, 2016
1 parent 1a817fc commit 8bd96c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
25 changes: 1 addition & 24 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function getFilters()

// encoding
new Twig_Filter('url_encode', 'twig_urlencode_filter'),
new Twig_Filter('json_encode', 'twig_jsonencode_filter'),
new Twig_Filter('json_encode', 'json_encode'),
new Twig_Filter('convert_encoding', 'twig_convert_encoding'),

// string filters
Expand Down Expand Up @@ -582,29 +582,6 @@ function twig_urlencode_filter($url)
return rawurlencode($url);
}

/**
* JSON encodes a variable.
*
* @param mixed $value The value to encode.
* @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
*
* @return mixed The JSON encoded value
*/
function twig_jsonencode_filter($value, $options = 0)
{
if ($value instanceof Twig_Markup) {
$value = (string) $value;
} elseif (is_array($value)) {
array_walk_recursive($value, function (&$value) {
if ($value instanceof Twig_Markup) {
$value = (string) $value;
}
});
}

return json_encode($value, $options);
}

/**
* Merges an array with another one.
*
Expand Down
7 changes: 6 additions & 1 deletion lib/Twig/Markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Markup implements Countable
class Twig_Markup implements Countable, JsonSerializable
{
private $content;
private $charset;
Expand All @@ -34,4 +34,9 @@ public function count()
{
return mb_strlen($this->content, $this->charset);
}

public function jsonSerialize()
{
return $this->content;
}
}

0 comments on commit 8bd96c1

Please sign in to comment.