Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » IF control on $ID / Counter

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

Page: 1
Go to End
Author Topic: 4151 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    IF control on $ID / Counter Link to this post

    Is there a way to compare the ID Value by size like this.

    <% if ID < 5 %>
    <li>$ID. $Name</li>
    <% end_if %>

    This code doesn't work but I need a comparison with bigger/smaller not only equal/unqual

    How to Implement a Counter inside the Template

    <% Counter = 0 %>
    <% control SomeCollection %>
    <% if Counter > 5 %>
    <li>$Attribut</li>
    <% end_if %>
    <% Counter++ %>
    <% end_control %>

    Is there a way to implement something like this?

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: IF control on $ID / Counter Link to this post

    Good morning Andre,

    At present, the template engine doesn't support greater than/less than operators. What you could do is create a method in your model class that does the comparison. You could then call this method from your template.

    Example template code:

    <% if IDGreaterThan(5) %>

    Ben

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: IF control on $ID / Counter Link to this post

    Hi, the Task I have to do is the Following.

    A List of Dataobjects should be splitted over two divs. To make it easyer for the moment I split them in the middle. The the fist half should be printed in Column1, the SecondHalf in Column two.

    I build the following Object and Controller with the methods FirstHalfProjectImages and LastHalfProjectImages.

    <?php

    class ProjectImagePage extends Page
    {
    static $has_many = array (
    'ProjectImages' => 'ProjectImage'
    );

    public function getCMSFields()
    {
    $f = parent::getCMSFields();
    $manager = new ImageDataObjectManager(
    $this, // Controller
    'ProjectImages', // Source name
    'ProjectImage', // Source class
    'Attachment', // File name on DataObject
    array(
    'Name' => 'Name',
    'Description' => 'Description'
    ), // Headings
    'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
    // Filter clause
    // Sort clause
    // Join clause
    );
    $f->addFieldToTab("Root.Content.Projects", $manager);
    return $f;
    }

    /*function ProjectImages(){
    $images = DataObject::get('ProjectImages');
    return $images;
    }*/
    }

    class ProjectImagePage_Controller extends Page_Controller{

    function FirstHalfProjectImages(){
    $ProjectImages = DataObject::get("ProjectImage");
    $half = round($ProjectImages->Count() / 2);

    $doSet = new DataObjectSet();

    $i = 1;
    foreach($ProjectImages as $ProjectImage){
    if($i <= $half){
    $doSet->push($ProjectImage);
    }
    $i++;
    }

    return $doSet;
    }

    function LastHalfProjectImages(){
    $ProjectImages = DataObject::get("ProjectImage");
    $half = round($ProjectImages->Count() / 2);

    $doSet = new DataObjectSet();

    $i = 1;
    foreach($ProjectImages as $ProjectImage){
    if($i > $half){
    $doSet->push($ProjectImage);
    }
    $i++;
    }

    return $doSet;
    }
    }

    ?>

    <?php

    class ProjectImage extends DataObject{
    static $db = array (
    'ProjektName' => 'Text',
    'Description' => 'Text'
    );

    static $has_one = array (
    'Attachment' => 'Image',
    'ProjectImagePage' => 'ProjectImagePage'
    );

    public function getCMSFields_forPopup(){
    return new FieldSet(
    new TextField('ProjektName'),
    new TextareaField('Description'),
    new ImageField('Attachment')
    );
    }
    }

    ?>

    Now my Problem: The Code is working finde, but I have two Pages that are of ProjectImagePage-Type and each should show an own collection of ProjectImages. But the First/LastHalfProjectImages Methods are always getting the full List out of the ProjectImage Table. How do I filter only the ones related to the current ProjectImagePage?

    Next Thing, I need ongoing Numbering for column1 and 2

    My Template looks the following:

    <div id="column1">
    <% control FirstHalfProjectImages %>
    <li><a href="#">$Pos. <span>$Description</span></a></li>
    <% end_control %>
    </div>

    <div id="column2">
    <% control LastHalfProjectImages %>
    <li><a href="#">$Pos. <span>$Description</span></a></li>
    <% end_control %>
    </div>

    But as you can imagine Numbering ($Pos) is starting with 1 again in Column2. How can I create and read from a variable to add its value to $Pos of Column2 so that it may starts with 6 if 5 ist the last $pos of Column1?

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: IF control on $ID / Counter Link to this post

    Found a solution for a Filter by myself

    $ProjectImages = DataObject::get("ProjectImage", "ProjectImage.ProjectImagePageID = {$this->ID}");

    The only question that stays is the counter for the $Pos Variable.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: IF control on $ID / Counter Link to this post

    The template engine does not support checking against dynamic fields / variables such as $Pos or ID. You will have to do something in the PHP code instead.

  • moloko_man
    Avatar
    Community Member
    72 Posts

    Re: IF control on $ID / Counter Link to this post

    I currently have big list of PDF links that will grow as time goes on and I have been able to split the list in half using the example from here http://silverstripe.org/data-model-questions/show/252268?start=0#post252326

    However I have the same problem with the numbering issue (on this thread). I have two columns, the left column's numbering is fine, but the second column starts over at 1, and my client insists on having the second column's numbers continue on.

    I realize that one cannot edit the $Pos number, however, is there a way to get the the number or count of the first column and then return a custom count number for the second column?

    I have tried using <% control firstColumn %>$Count<% end_control %> inside the <% control secondColumn %> but nothing returns.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: IF control on $ID / Counter Link to this post

    If you know the maximum number in the fist column (lets say 10) you could create a function like

    public function newPos(){
    $currentPos = $this->Pos;
    if ($currentPos > 10){
    return $currentPos + 10;
    } else {
    return $currentPos;
    }
    }
    (Though Pos may nee dto be checked with $this->Pos(); )
    Just drop that on your dataobject

    4151 Views
Page: 1
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.