Skip to content

Commit

Permalink
merged branch Tobion/buildquery (PR twigphp#1009)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the master branch (closes twigphp#1009).

Commits
-------

fcf9525 let url_encode filter also accept array of query parameters

Discussion
----------

let url_encode filter also accept array of query parameters

BC break: no
feature addition: yes
tests pass: yes
documentation: yes

This is also supported in Jinja and so it's consistent: [jinja urlencode](http://modular.math.washington.edu/home/wstein/www/home/bjarke/sage-4.4.4/local/LIB/python/site-packages/Jinja-1.2-py2.6-linux-x86_64.egg/docs/html/builtins.html)
  • Loading branch information
fabpot committed Feb 26, 2013
2 parents 7b11c6f + fcf9525 commit bc30d9b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
15 changes: 12 additions & 3 deletions doc/filters/url_encode.rst
Original file line number Diff line number Diff line change
@@ -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
10 changes: 7 additions & 3 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions test/Twig/Tests/Fixtures/filters/urlencode.test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bc30d9b

Please sign in to comment.