Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Is there a control to detect every nth list item

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 6926 Views
  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Awesome, that worked

    My only concern about adding it into the /sapphire/code/ViewableData.php is that when I go to update the SS with another release, this could easily be forgotten about. Is there a way to extend this?

    I've tried adding the following into my _config.php

    Object::add_extension('ViewableData', 'ExtendedViewableData');

    and in /mysite/code/ExtenedViewableData.php

    <?php
    class ExtendedViewableData extends Extension {
       
       /**
        * Returns 'true' if this item is after 4th item.
        * @return boolean
        */
       function IsAfterFourth() {
          return $this->iteratorPos % 4 == 0;
       }
       
    }
    ?>

    But this doesn't seem to work and I get the following error:

    "[User Error] Object::__call() Method 'loadExtraStatics' not found in class 'ExtendedViewableData'"

    Have I got this all wrong and need to approach it from another angle?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Is there a control to detect every nth list item Link to this post

    There is absolutely no reason why you should have to hack the core to do this. Add the function I gave you to the object you're controlling. For instance:

    <% control MyObject %>
    <% if Fourth %>Fourth<% end_if %>
    <% end_control %>

    class MyObject
    {
    function Fourth()
    {
    return !$this->iteratorPos % 4;
    }
    }

  • Trym
    Avatar
    Community Member
    18 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Hi UncleCheese.

    If the object that is in control is a DataObjectSet returned from e.g. the method call "DataObject::get", then its more troblesome to add the desired functionality.

    Do you have a suggestion for this?

    Best regards Trym

  • Hammy
    Avatar
    Community Member
    49 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Sorry - should have mentioned that I'm using DataObject::get method as the object inside the control...

  • UncleCheese
    Avatar
    4085 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Right.. so this:

    class SomePage extends Page
    {
    function Objects()
    {
    return DataObject::get("SomeObject");
    }
    }

    class SomeObject
    {
    function Fourth() { return !$this->iteratorPos % 4;}
    }

    <% control Objects %>
    <% if Fourth %>fourth<% end_if %>
    <% end_control %>

    Shouldn't need anything more than that. I do this all the time.

  • Trym
    Avatar
    Community Member
    18 Posts

    Re: Is there a control to detect every nth list item Link to this post

    cool.

    \trym

  • reekrd
    Avatar
    Community Member
    9 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Hi.

    I'm trying to apply this to the second tutorial, http://doc.silverstripe.org/doku.php?id=tutorial:2-extending-a-basic-site

    My setup is exactly as in the tutorial and for every third news item I would like to add a divider or set a special css class. I just can't figure it out

    This is how my HomePage.ss looks like:

    <div id="NewsWrapper" class="content {$URLSegment}">
       <ul id="NewsList">
       <% control LatestNews %>
          <li class="newsitem">
             <h1><a href="$Link" title="Read more on &quot;{$Name}&quot;">$Title</a></h1>
             <h3>$Date.Nice</h3>
             <p>$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>
          </li>
       <% end_control %>
       </ul>
    </div> <!-- end: NewsWrapper -->

    In this thread I saw that there is a template variable called $Pos that gives you the current position. However I can't figure out how to use it. It seems logical that I could simply do:

    <% control LatestNews %>
       <% if (($Pos%3)==0) %>
          <li class="newsitem">
       <% else %>
          <li class="newsitem">
       <% end_if %>
          <h1><a href="$Link" title="Read more on &quot;{$Name}&quot;">$Title</a></h1>
          <h3>$Date.Nice</h3>
          <p>$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>
       </li>
    <% end_control %>

    But that gives me a blank page. I can't seem to use it in any way when I combine it with an if statement. It would be awesome if the tutorials also would cover things like this. Tutorials that are more from a designers point of view.

    Maybe there is a section in the documentation or tutorials that covers things like this. If not it would be very helpful to collect all these types of tips and tricks so it's easier for a designer to jump straight into using SilverStripe without really having to understand the inner workings of it....

    Best
    .r

  • UncleCheese
    Avatar
    4085 Posts

    Re: Is there a control to detect every nth list item Link to this post

    Assuming you're using the same classes that are in the tutorial, you can just add to your ArticlePage class:

    public function Third()
    {
    return !$this->iteratorPos % 3;
    }

    then replace:

    <% if (($Pos%3)==0) %>

    with

    <% if Third %>

    6926 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.