mirror of
https://github.com/twigphp/Twig.git
synced 2026-11-12 05:16:38 +00:00
Added support for css_class function
This commit is contained in:
committed by
Fabien Potencier
parent
ad2ddd68ef
commit
2200d837ef
@@ -118,6 +118,7 @@
|
||||
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)
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
``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,
|
||||
}) }}
|
||||
@@ -7,6 +7,7 @@ Functions
|
||||
attribute
|
||||
block
|
||||
constant
|
||||
css_class
|
||||
cycle
|
||||
date
|
||||
dump
|
||||
|
||||
@@ -269,6 +269,7 @@ 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'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1548,4 +1549,29 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
--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
|
||||
@@ -0,0 +1,8 @@
|
||||
--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.
|
||||
@@ -0,0 +1,8 @@
|
||||
--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.
|
||||
Reference in New Issue
Block a user