SilverStripe 连续获得第一个孩子

发布于 2024-12-17 13:39:51 字数 394 浏览 3 评论 0原文

我有多个孩子,我将它们显示在网格中。 4 个孩子排成一排。现在,我想为连续的每个第一个和最后一个子元素提供一个额外的类来指定更多样式。我试过:

<% if FirstInRow %>
    <div class="gridContent firstInRow"></div>
<% else %>
    <div class="gridContent"></div>
<% end_if %>

这就是功能:

function FirstInRow(){
    return ($this->Pos(1) % 4 == 1) ? true : false;
}

I have got multiple children which I display in a grid. 4 children fits in a row. Now I want to give every first and last children in a row an extra class to specify more styles. I tried:

<% if FirstInRow %>
    <div class="gridContent firstInRow"></div>
<% else %>
    <div class="gridContent"></div>
<% end_if %>

That's the function:

function FirstInRow(){
    return ($this->Pos(1) % 4 == 1) ? true : false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-12-24 13:39:51

您可以在模板中使用 ModulusMultipleOf 控件。

$Modulus(value, offset) // returns an int
$MultipleOf(factor, offset) // returns a boolean.

http://doc.silverstripe.org/sapphire/en /reference/advanced-templates#modulus-and-multipleof

You have the Modulus and MultipleOf controls available to you in the template.

$Modulus(value, offset) // returns an int
$MultipleOf(factor, offset) // returns a boolean.

http://doc.silverstripe.org/sapphire/en/reference/advanced-templates#modulus-and-multipleof

沉溺在你眼里的海 2024-12-24 13:39:51

当您可以从 0 开始时,模数效果会更好。请尝试这样做:

function FirstInRow(){
    return ($this->Pos(0) % 4 == 0);
}

请注意,我还删除了冗余的 ternay 运算符;如果它使代码对您来说更清晰,您可以将其保留。

Modulus works better when you could starting from 0. Try this instead:

function FirstInRow(){
    return ($this->Pos(0) % 4 == 0);
}

Note that I removed the redunandant ternay operator as well; you can leave that in if it makes the code clearer to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文