From fcf9525dc3e96aaecf8566a1a9e0a9335d6b515a Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 22 Feb 2013 10:17:24 +0100 Subject: [PATCH] let url_encode filter also accept array of query parameters --- CHANGELOG | 1 + doc/filters/url_encode.rst | 15 ++++++++++++--- lib/Twig/Extension/Core.php | 10 +++++++--- test/Twig/Tests/Fixtures/filters/urlencode.test | 12 ++++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/urlencode.test diff --git a/CHANGELOG b/CHANGELOG index 5cd3a65..8618b64 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ * 1.12.3 (2013-XX-XX) * added a batch filter + * added support for encoding an array as query string in the url_encode filter * 1.12.2 (2013-02-09) diff --git a/doc/filters/url_encode.rst b/doc/filters/url_encode.rst index c141f3b..d3a33fd 100644 --- a/doc/filters/url_encode.rst +++ b/doc/filters/url_encode.rst @@ -1,14 +1,23 @@ ``url_encode`` ============== -The ``url_encode`` filter URL encodes a given string: +.. versionadded:: 1.12.3 + Support for encoding an array as query string was added in Twig 1.12.3. + +The ``url_encode`` filter percent encodes a given string as URL segment +or an array as query string: .. code-block:: jinja - {{ data|url_encode() }} + {{ "path-seg*ment"|url_encode }} + {# outputs "path-seg%2Ament" #} + + {{ {'param': 'value', 'foo': 'bar'}|url_encode }} + {# outputs "param=value&foo=bar" #} .. note:: - Internally, Twig uses the PHP `urlencode`_ function. + Internally, Twig uses the PHP `urlencode`_ or the `http_build_query`_ function. .. _`urlencode`: http://php.net/urlencode +.. _`http_build_query`: http://php.net/http_build_query diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index a856b59..a439571 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -515,15 +515,19 @@ function twig_number_format_filter(Twig_Environment $env, $number, $decimal = nu } /** - * URL encodes a string. + * URL encodes a string as a path segment or an array as a query string. * - * @param string $url A URL - * @param bool $raw true to use rawurlencode() instead of urlencode + * @param string|array $url A URL or an array of query parameters + * @param bool $raw true to use rawurlencode() instead of urlencode * * @return string The URL encoded value */ function twig_urlencode_filter($url, $raw = false) { + if (is_array($url)) { + return http_build_query($url, '', '&'); + } + if ($raw) { return rawurlencode($url); } diff --git a/test/Twig/Tests/Fixtures/filters/urlencode.test b/test/Twig/Tests/Fixtures/filters/urlencode.test new file mode 100644 index 0000000..de956e7 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/urlencode.test @@ -0,0 +1,12 @@ +--TEST-- +"url_encode" filter +--TEMPLATE-- +{{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode }} +{{ {foo: "bar", number: 3, "spéßi%l": "e%c0d@d", "spa ce": ""}|url_encode|raw }} +{{ {}|url_encode|default("default") }} +--DATA-- +return array() +--EXPECT-- +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa+ce= +foo=bar&number=3&sp%C3%A9%C3%9Fi%25l=e%25c0d%40d&spa+ce= +default -- 1.7.2.5