The Twig CssColorFromString bundle extends Twig with a simple filter converting strings to a css color.
<div class="badge" style="color: #222; background-color: {{ tag|hex }};">{{ tag }}</div>
The hex
filter converts the tag
Standard to the css color #747F3F
. The output will be
<div class="badge" style="color: #222; background-color: #747F3F;">Standard</div>
Thanks to the stackoverflow developer Reinderien who's post https://stackoverflow.com/a/3724219 is the fundamental of this plugin.
composer require fincallorca/twig-css-color-from-string "dev-master"
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
// [...]
new Fincallorca\TwigExtension\CssColorFromStringBundle\CssColorFromStringBundle(),
];
}
}
The hex
filter converts a string to a css color.
The same string will be converted every time in the same css color (similar like a hash value).
The optional parameter describe the saturation and lightness and can have values
between 0 and 1. The example below uses a saturation
of 0.5
and a lightness
of 0.9
.
If no parameters are submitted 0.5
is used for each parameter.
<div class="badge" style="color: #222; background-color: {{ tag|hex(0.5, 0.9) }};">{{ tag }}</div>