From 932a4d8d943238b6d799c471f420f6893163d3fa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 18 Dec 2011 12:28:42 +0100 Subject: [PATCH] added a nl2br filter --- CHANGELOG | 1 + doc/filters/index.rst | 1 + doc/filters/nl2br.rst | 19 +++++++++++++++++++ lib/Twig/Extension/Core.php | 1 + test/Twig/Tests/Fixtures/filters/nl2br.test | 14 ++++++++++++++ 5 files changed, 36 insertions(+), 0 deletions(-) create mode 100644 doc/filters/nl2br.rst create mode 100644 test/Twig/Tests/Fixtures/filters/nl2br.test diff --git a/CHANGELOG b/CHANGELOG index 00be5eb..175e98a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0 + * added a nl2br filter * added a random function * added a way to change the default format for the date filter * fixed the lexer when an operator ending with a letter ends a line diff --git a/doc/filters/index.rst b/doc/filters/index.rst index 599f2cf..6593a06 100644 --- a/doc/filters/index.rst +++ b/doc/filters/index.rst @@ -12,6 +12,7 @@ Filters convert_encoding title capitalize + nl2br upper lower striptags diff --git a/doc/filters/nl2br.rst b/doc/filters/nl2br.rst new file mode 100644 index 0000000..366ca83 --- /dev/null +++ b/doc/filters/nl2br.rst @@ -0,0 +1,19 @@ +``nl2br`` +========= + +The ``nl2br`` filter inserts HTML line breaks before all newlines in a string: + +.. code-block:: jinja + + {{ "I like Twig.\nYou will like it too."|nl2br }} + {# outputs + + I like Twig.
+ You will like it too. + + #} + +.. note:: + + The ``nl2br`` filter pre-escapes the input before applying the + transformation. diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index d6bd7b8..9e5cf20 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -83,6 +83,7 @@ class Twig_Extension_Core extends Twig_Extension 'upper' => new Twig_Filter_Function('strtoupper'), 'lower' => new Twig_Filter_Function('strtolower'), 'striptags' => new Twig_Filter_Function('strip_tags'), + 'nl2br' => new Twig_Filter_Function('nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), // array helpers 'join' => new Twig_Filter_Function('twig_join_filter'), diff --git a/test/Twig/Tests/Fixtures/filters/nl2br.test b/test/Twig/Tests/Fixtures/filters/nl2br.test new file mode 100644 index 0000000..6545a9b --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/nl2br.test @@ -0,0 +1,14 @@ +--TEST-- +"nl2br" filter +--TEMPLATE-- +{{ "I like Twig.\nYou will like it too.\n\nEverybody like it!"|nl2br }} +{{ text|nl2br }} +--DATA-- +return array('text' => "If you have some HTML\nit will be escaped.") +--EXPECT-- +I like Twig.
+You will like it too.
+
+Everybody like it! +If you have some <strong>HTML</strong>
+it will be escaped. -- 1.7.2.5