Skip to content

Commit

Permalink
feature #2403 #1802 left/right trim (antonydandrea)
Browse files Browse the repository at this point in the history
This PR was submitted for the 2.x branch but it was merged into the 1.x branch instead (closes #2403).

Discussion
----------

#1802 left/right trim

It was said in this thread that there was a preference of adding parameters to the trim filter in order to implement this.

So this feature works like:

trim(character_mask, mode), where mode is either "both", "left" or "right". It is by default "both".

This is my first pull request of this project, so I apologise in advance if something is wrong.

Commits
-------

7d8e755 #1802 left/right trim
  • Loading branch information
fabpot committed Feb 23, 2017
2 parents 30085c5 + 7d8e755 commit c84a511
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* 1.32.0 (2017-XX-XX)

* added a PSR-11 compatible runtime loader
* added `side` argument to `trim` to allow left or right trimming only.

* 1.31.0 (2017-01-11)

Expand Down
18 changes: 17 additions & 1 deletion doc/filters/trim.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
``trim``
========
.. versionadded:: 1.32

The ``side`` argument was added in Twig 1.32.

.. versionadded:: 1.6.2
The ``trim`` filter was added in Twig 1.6.2.
Expand All @@ -17,13 +20,26 @@ and end of a string:
{# outputs ' I like Twig' #}
{{ ' I like Twig. '|trim(side='left') }}
{# outputs 'I like Twig. ' #}
{{ ' I like Twig. '|trim(' ', 'right') }}
{# outputs ' I like Twig.' #}
.. note::

Internally, Twig uses the PHP `trim`_ function.
Internally, Twig uses the PHP `trim`_, `ltrim`_, and `rtrim`_ functions.

Arguments
---------

* ``character_mask``: The characters to strip

* ``side``: The default is to strip from the left and the right (`both`) sides, but `left`
and `right` will strip from either the left side or right side only

.. _`trim`: http://php.net/trim
.. _`ltrim`: http://php.net/ltrim
.. _`rtrim`: http://php.net/rtrim
27 changes: 26 additions & 1 deletion lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getFilters()
new Twig_SimpleFilter('upper', 'strtoupper'),
new Twig_SimpleFilter('lower', 'strtolower'),
new Twig_SimpleFilter('striptags', 'strip_tags'),
new Twig_SimpleFilter('trim', 'trim'),
new Twig_SimpleFilter('trim', 'twig_trim_filter'),
new Twig_SimpleFilter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),

// array helpers
Expand Down Expand Up @@ -945,6 +945,31 @@ function twig_in_filter($value, $compare)
return false;
}

/**
* Returns a trimmed string.
*
* @return string
*
* @throws Twig_Error_Runtime When an invalid trimming side is used (not a string or not 'left', 'right' or 'both')
*/
function twig_trim_filter($string, $characterMask = null, $side = 'both')
{
if (null === $characterMask) {
$characterMask = " \t\n\r\0\x0B";
}

switch ($side) {
case 'both':
return trim($string, $characterMask);
case 'left':
return ltrim($string, $characterMask);
case 'right':
return rtrim($string, $characterMask);
default:
throw new Twig_Error_Runtime('Trimming side must be "left", "right" or "both".');
}
}

/**
* Escapes a string.
*
Expand Down
12 changes: 12 additions & 0 deletions test/Twig/Tests/Fixtures/filters/trim.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
{{ " I like Twig. "|trim }}
{{ text|trim }}
{{ " foo/"|trim("/") }}
{{ " I like Twig. "|trim(side="left") }}
{{ " I like Twig. "|trim(side="right") }}
{{ " I like Twig. "|trim(null, "right") }}
{{ "/ foo/"|trim("/", "left") }}
{{ "/ foo/"|trim(character_mask="/", side="left") }}
{{ " do nothing. "|trim("", "right") }}
--DATA--
return array('text' => " If you have some <strong>HTML</strong> it will be escaped. ")
--EXPECT--
I like Twig.
If you have some &lt;strong&gt;HTML&lt;/strong&gt; it will be escaped.
foo
I like Twig.
I like Twig.
I like Twig.
foo/
foo/
do nothing.

0 comments on commit c84a511

Please sign in to comment.