moved css_class function to html_classes filter

This commit is contained in:
Fabien Potencier
2019-08-07 15:57:18 +02:00
parent 2200d837ef
commit 6ce9beb9fd
12 changed files with 94 additions and 70 deletions
+1 -1
View File
@@ -1,5 +1,6 @@
* 2.12.0 (2019-XX-XX)
* added the `html_classes` function
* added the "data_uri" filter
* fixed cache when opcache is installed but disabled
* fixed using macros in arrow functions
@@ -118,7 +119,6 @@
Twig\Environment::setBaseTemplateClass() methods
* changed internal code to use the namespaced classes as much as possible
* deprecated Twig_Parser::isReservedMacroName()
* Added support for `css_class` function
* 2.6.2 (2019-01-14)
-12
View File
@@ -1,12 +0,0 @@
``css_class``
=============
``css_class`` returns a string by conditionally joining class names together.
.. code-block:: jinja
{{ css_class('a-class', 'another-class', {
'errored': object.errored,
'finished': object.finished,
'pending': object.pending,
}) }}
+26
View File
@@ -0,0 +1,26 @@
``html_classes``
================
.. versionadded:: 2.12
The ``html_classes`` function was added in Twig 2.12.
The ``html_classes`` function returns a string by conditionally joining class
names together:
.. code-block:: jinja
<p class="{{ html_classes('a-class', 'another-class', {
'errored': object.errored,
'finished': object.finished,
'pending': object.pending,
}) }}">How are you doing?</p>
.. note::
The ``html_classes`` function is part of the ``HtmlExtension`` which is not
enabled by default; you must add it explicitly on the Twig environment::
use Twig\Extension\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
+1 -1
View File
@@ -7,10 +7,10 @@ Functions
attribute
block
constant
css_class
cycle
date
dump
html_classes
include
max
min
-26
View File
@@ -269,7 +269,6 @@ final class CoreExtension extends AbstractExtension
new TwigFunction('date', 'twig_date_converter', ['needs_environment' => true]),
new TwigFunction('include', 'twig_include', ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]),
new TwigFunction('source', 'twig_source', ['needs_environment' => true, 'is_safe' => ['all']]),
new TwigFunction('css_class', 'twig_css_class'),
];
}
@@ -1549,29 +1548,4 @@ function twig_array_reduce($array, $arrow, $initial = null)
return array_reduce($array, $arrow, $initial);
}
function twig_css_class(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (is_string($arg)) {
$classes[] = $arg;
} elseif (is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!is_string($class)) {
throw new RuntimeError(sprintf('Key %d of argument %d should be a string. Got "%s".', $class, $i, gettype($class)));
}
if (!$condition) {
continue;
}
$classes[] = $class;
}
} else {
throw new RuntimeError(sprintf('Argument %d should be either a string or an array. Got "%s".', $i, gettype($arg)));
}
}
$classes = array_unique($classes);
return implode(' ', $classes);
}
}
+38 -2
View File
@@ -9,10 +9,10 @@
* file that was distributed with this source code.
*/
namespace Twig\Extension;
namespace Twig\Extension {
use Symfony\Component\Mime\MimeTypes;
use Twig\TwigFilter;
use Twig\TwigFunction;
final class HtmlExtension extends AbstractExtension
{
@@ -30,6 +30,13 @@ final class HtmlExtension extends AbstractExtension
];
}
public function getFunctions()
{
return [
new TwigFunction('html_classes', 'twig_html_classes'),
];
}
/**
* Creates a data URI (RFC 2397).
*
@@ -77,3 +84,32 @@ final class HtmlExtension extends AbstractExtension
return $repr;
}
}
}
namespace {
use Twig\Error\RuntimeError;
function twig_html_classes(...$args): string
{
$classes = [];
foreach ($args as $i => $arg) {
if (is_string($arg)) {
$classes[] = $arg;
} elseif (is_array($arg)) {
foreach ($arg as $class => $condition) {
if (!is_string($class)) {
throw new RuntimeError(sprintf('The html_classes function argument %d (key %d) should be a string, got "%s".', $i, $class, gettype($class)));
}
if (!$condition) {
continue;
}
$classes[] = $class;
}
} else {
throw new RuntimeError(sprintf('The html_classes function argument %d should be either a string or an array, got "%s".', $i, gettype($arg)));
}
}
return implode(' ', array_unique($classes));
}
}
@@ -1,12 +0,0 @@
--TEST--
"css_class" function
--TEMPLATE--
{{ css_class('a', {'b': true, 'c': false}, 'd') }}
{% set class_a = 'a' %}
{% set class_b = 'b' %}
{{ css_class(class_a, {(class_b): true})}}
--DATA--
return []
--EXPECT--
a b d
a b
@@ -1,8 +0,0 @@
--TEST--
"css_class" function
--TEMPLATE--
{{ css_class(true) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Argument 0 should be either a string or an array. Got "boolean" in "index.twig" at line 2.
@@ -1,8 +0,0 @@
--TEST--
"css_class" function
--TEMPLATE--
{{ css_class(['foo']) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: Key 0 of argument 0 should be a string. Got "integer" in "index.twig" at line 2.
@@ -0,0 +1,12 @@
--TEST--
"html_classes" function
--TEMPLATE--
{{ html_classes('a', {'b': true, 'c': false}, 'd') }}
{% set class_a = 'a' %}
{% set class_b = 'b' %}
{{ html_classes(class_a, {(class_b): true})}}
--DATA--
return []
--EXPECT--
a b d
a b
@@ -0,0 +1,8 @@
--TEST--
"html_classes" function
--TEMPLATE--
{{ html_classes(true) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The html_classes function argument 0 should be either a string or an array, got "boolean" in "index.twig" at line 2.
@@ -0,0 +1,8 @@
--TEST--
"html_classes" function
--TEMPLATE--
{{ html_classes(['foo']) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The html_classes function argument 0 (key 0) should be a string, got "integer" in "index.twig" at line 2.