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

IF control on $ID / Counter


Reply

7 Posts   4167 Views

Avatar
Andre

18 April 2009 at 12:03am (Last edited: 18 April 2009 12:21am), Community Member, 126 Posts

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

18 April 2009 at 1:13am Community Member, 181 Posts

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

21 April 2009 at 2:20am Community Member, 126 Posts

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

21 April 2009 at 4:14am Community Member, 126 Posts

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

21 April 2009 at 6:19pm Forum Moderator, 5511 Posts

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

30 December 2009 at 9:16am Community Member, 72 Posts

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 [url]http://silverstripe.org/data-model-questions/show/252268?start=0#post252326[/url]

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

23 January 2010 at 3:18am Community Member, 528 Posts

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