Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

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

IF control on $ID / Counter


Go to End
Reply


7 Posts   4355 Views

Avatar
Andre

Community Member, 133 Posts

18 April 2009 at 12:03am

Edited: 18/04/2009 12:21am

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?

Avatar
Ben Gribaudo

Community Member, 181 Posts

18 April 2009 at 1:13am

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

Avatar
Andre

Community Member, 133 Posts

21 April 2009 at 2:20am

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?

Avatar
Andre

Community Member, 133 Posts

21 April 2009 at 4:14am

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.

Avatar
Willr

Forum Moderator, 5514 Posts

21 April 2009 at 6:19pm

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.

Avatar
moloko_man

Community Member, 72 Posts

30 December 2009 at 9:16am

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.

Avatar
zenmonkey

Community Member, 532 Posts

23 January 2010 at 3:18am

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