korcontrol / craft-classy
Twig helpers inspired by https://github.com/JedWatson/classnames
Installs: 7 018
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Type:craft-plugin
pkg:composer/korcontrol/craft-classy
Requires
- craftcms/cms: ^5.0.0
Requires (Dev)
- craftcms/rector: dev-main
This package is not auto-updated.
Last update: 2025-10-18 12:23:28 UTC
README
Twig helpers inspired by https://github.com/JedWatson/classnames
Usage
Filter
{% apply class(
"add-this-initially",
{
"add-this": true,
"remove-this": false,
"neither-add-nor-remove": null,
"add-or-remove": ifthis and ifthat,
"add-but-dont-remove": condition ? true : null,
"remove-but-dont-add": not condition ? false : null,
},
"add-this-regardless-of-above",
) %}
<mytag class="existing classes"> ... </mytag>
{% endapply %}
Function
<mytag class="{{ class("yup yes", { "maybe": ifthis and ifthat }) }}">
Which depending on the conditions ifthis and ifthat will give
<mytag class="yup yes maybe"> or <mytag class="yup yes">
Each argument can be either a string or a map.
A string argument will be treated as a class name (or multiple space-separated class names) to add.
An array argument is a map of class names to statuses.
Its keys are single class names or groups of space-seprated classes. The corresponding values can be
- strictly
trueto add the class or classes - strictly
falseto remove the class or classes nullor anything else to leave them alone
Any existing classes which aren't mentioned in any of these arguments are left alone.
Arguments are processed in order.