英文:
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 '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.
答案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 '<div style="grid-row: span ' . $variable . ' / span ' . $variable . ';"></div>';
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) => {
const span = i + 7;
return [
`span-${span}`,
`span ${span} / span ${span}`,
];
}),
),
},
},
// …
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论