【问题标题】:$Pos from bottom inside <% loop DataObjects %>$Pos 从底部 <% loop DataObjects %>
【发布时间】:2012-12-15 07:05:09
【问题描述】:

可以在模板中的循环内通过 DataObject 以某种方式告诉您是否处于 $Pos 24 但从底部开始计数 - 类似于:

<% if Pos = "-24" %>do stuff<% end_if %>
or like
<% if TotalItems - 24 = Pos %>do stuff<% end_if %>
or like
<% if Last(24) %>do stuff<% end_if %>

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    在 Silverstripe 3 中,能够做到:

    <% if FromBottom(24) %>
      Hello
    <% end_if %>
    

    您必须将 MyCustomIteratorFunctions.php 添加到您的 /code 文件夹中,其中包含以下内容:

    <?php
    class MyCustomIteratorFunctions implements TemplateIteratorProvider
    {
    
      protected $iteratorPos;
      protected $iteratorTotalItems;
    
      public static function get_template_iterator_variables()
      {
        return array('FromBottom');
      }
    
      public function iteratorProperties($pos, $totalItems)
      {
        $this->iteratorPos = $pos;
        $this->iteratorTotalItems = $totalItems;
      }
    
      function FromBottom($num)
      {
        return (($this->iteratorTotalItems - $this->iteratorPos) == $num);
      }
    }
    

    如此处所示:https://groups.google.com/forum/#!msg/silverstripe-dev/DVBtzkblZqA/PWxanKGKDYIJ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 2012-10-23
      • 1970-01-01
      相关资源
      最近更新 更多