I orginally wanted to use $Top.squarecolor($ID) from a template to call a function that calculated what colour to use from the $ID but I understand this is not possible and that I should be 'pushing this into the model' Question is how.
I have added an additional coloumn to the model to hold the information
public static $db = array(
"sqcolor" => "enum('yellow, orange, grey, blue','blue')",
)
and then added a function to the appropriate controller
function Squares() {
$SquaresDetails = DataObject::get("SquaresPage");
foreach($SquaresDetails as $SquareDetail){
if ($ID%4==1){
$SquareDetail->sqcolor = "blue";}
elseif ($ID%4==2){
$SquareDetail->sqcolor = "yellow";}
elseif ($ID%4==3){
$SquareDetail->sqcolor = "orange";}
else {$SquareDetail->sqcolor = "grey";}}
return DataObject::get("SquaresPage");
}
and then within the template I call that function
<% control Squares %>
<div class ="square $sqcolor">
$Content
</div>
<% end_control %>
but the Squares function does not update the sqcolor field as I expected.
What am I doing wrong?
Thanks
MM