korcontrol/craft-classy

受 https://github.com/JedWatson/classnames 启发的 Twig 辅助函数

2.0.1 2023-08-14 15:54 UTC

This package is not auto-updated.

Last update: 2024-09-23 21:08:20 UTC


README

https://github.com/JedWatson/classnames 启发的 Twig 辅助函数

用法

过滤器

{% 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 %}

函数

<mytag class="{{ class("yup yes", { "maybe": ifthis and ifthat }) }}">

根据条件 ifthisifthat 将给出的

<mytag class="yup yes maybe"> or <mytag class="yup yes">

每个参数可以是字符串或映射。

字符串参数将被视为要添加的类名(或多个由空格分隔的类名)。

数组参数是一个类名到状态的映射。

其键是单个类名或由空格分隔的类名组。对应的值可以是

  • 严格 true 以添加类或类
  • 严格 false 以删除类或类
  • null 或其他任何内容以保持不变

任何在上述任何参数中未提到的现有类都将保持不变。

参数按顺序处理。