Is there a way to dynamically apply the 'row-span-y' Tailwind CSS utility class based on a PHP variable in Laravel Blade?

huangapple go评论65阅读模式
英文:

Is there a way to dynamically apply the 'row-span-y' Tailwind CSS utility class based on a PHP variable in Laravel Blade?

问题

I need to apply the 'row-span-y' Tailwind CSS utility class on a div that is part of a grid layout based on a PHP variable that has a value between 2 and 16, let's say.

So far, the only possible way that I've found that actually works is doing this:

$variable = 3;
switch ($variable) :
    case 2:
        echo '<div class="row-span-2">';
        break;
    case 3:
        echo '<div class="row-span-3">';
        break;
    case 4:
        echo '<div class="row-span-4">';
        break;
    case 5:
        echo '<div class="row-span-5">';
        break;
...
endswitch;

echo '</div>';

I don't want to do that. What I would like to do instead is:

$variable = 3;
echo '<div class="row-span-' . $variable . '"></div>';

But that doesn't seem to work with Tailwind CSS. The utility class is not recognized.

英文:

I need to apply the &#39;row-span-y&#39; Tailwind CSS utility class on a div that is part of a grid-layout based on a PHP variable that has a value between 2 and 16, let's say.

So far, the only possible way that I've found that actually works is doing this:

$variable = 3;
switch ($variable) :
    case 2:
        echo &#39;&lt;div class=&quot;row-span-2&quot;&gt;&#39;;
        break;
    case 3:
        echo &#39;&lt;div class=&quot;row-span-3&quot;&gt;&#39;;
        break;
    case 4:
        echo &#39;&lt;div class=&quot;row-span-4&quot;&gt;&#39;;
        break;
    case 5:
        echo &#39;&lt;div class=&quot;row-span-5&quot;&gt;&#39;;
        break;
...
endswitch;

echo &#39;&lt;/div&gt;&#39;

I don't want to do that. What I would like to do instead is:

$variable = 3;
echo &#39;&lt;div class=&quot;row-span-&#39; . $variable . &#39;&quot;&gt;&lt;/div&gt;&#39;;

But that doesn't seem to work with Tailwind CSS. The utility class is not recognized.

答案1

得分: 0

你可以考虑使用style属性,例如:

$variable = 3;
echo '<div style="grid-row: span ' . $variable . ' / span ' . $variable . ';"></div>';

或者,由于您似乎知道可能的值的详尽列表,您可以safelist这些类,例如:

module.exports = {
  // …
  safelist: [
    { pattern: /^row-span-(1[0-6]|[2-9])$/ },
  ],
  theme: {
    extend: {
      // 添加额外的row-span-*,从7到16,因为它们在默认的Tailwind配置中不存在。
      gridRow: Object.fromEntries(
        Array(10).fill().map((_, i) => {
          const span = i + 7;
          return [
            `span-${span}`,
            `span ${span} / span ${span}`,
          ];
        }),
      ),
    },
  },
  // …
}
英文:

You could consider using the style attribute, like:

$variable = 3;
echo &#39;&lt;div style=&quot;grid-row: span &#39; . $variable . &#39; / span &#39; . $variable . &#39;;&quot;&gt;&lt;/div&gt;&#39;;

Alternatively, since you seem to have knowledge of the exhaustive list of possible values, you could safelist the classes, like:

module.exports = {
  // …
  safelist: [
    { pattern: /^row-span-(1[0-6]|[2-9])$/ },
  ],
  theme: {
    extend: {
      // Add extra row-span-* from 7 to 16 since they do not exist in the
      // default Tailwind configuration.
      gridRow: Object.fromEntries(
        Array(10).fill().map((_, i) =&gt; {
          const span = i + 7;
          return [
            `span-${span}`,
            `span ${span} / span ${span}`,
          ];
        }),
      ),
    },
  },
  // …
}

huangapple
  • 本文由 发表于 2023年5月26日 00:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定