0
votes

I'm trying to write a function that displays images from the child pages on the Holder Page.

Because SilverStripe lacks some functionality on templates I figured it will be best to process it all in the controller.

There are some conditional statements I require which can only be done in php.

Controller.php

public function LatestWork() {

$works = WorkPage::get();

$i = 1;
$html = "";
foreach ($works as $work) {

  //Build the IMage Object so we can add it to the Work Object
  $ImageObj = File::get()->byID($work->FeaturedImageID);

  if ($this->is_odd($i)) {
    $html .= "<div class='row'>";
    $span = "span8";
  } else {
    $span = "span4";
  }
  $html .= "<div class = '$span'>" . $ImageObj->croppedImage(200,100)  . "</div>";
  if ($this->is_even($i)  || $i == $works->Count()) {
    $html .= "</div>";
  }
  $i++;
}

return $html;
}

When its processed in the view the divs and spans are there but the image is not. There are more conditions in the code but this is just the basic version. It displays "Image_Cached" instead.

How can I make it display the image?

1
When you return $ImageObj->croppedImage(200,100) without the html or anything else eg: return $ImageObj->croppedImage(200,100) . " " it displays the image but as soon as you add anything it display Image_cache - user742736

1 Answers

1
votes

Controller:

public function LatestWork() {
    $rows=new ArrayList();
    foreach(WorkPage::get() as $workPage){
        if (!isset($bucket)){
              $bucket = new ArrayList();
              $bucket->push($workPage);
              $rows->push($bucket);
        } else {
              $bucket->push($workPage);
              unset($bucket);
        }
    }
    return $rows;
}

Template:

<% loop LatestWork %>
    <div class="row">
    <% if Odd %>
        <div class="span-8">
            <% with $Me.First %>$FeaturedImage.CroppedImage(200,100)<% end_with %>
        </div>
        <div class="span-4">
            <% with $Me.Last %>$FeaturedImage.CroppedImage(100,50)<% end_with %>
        </div>
    <% else %>
        <div class="span-4">
            <% with $Me.First %>$FeaturedImage.CroppedImage(100,50)<% end_with %>
        </div>
        <div class="span-8">
            <% with $Me.Last %>$FeaturedImage.CroppedImage(200,100)<% end_with %>
        </div>
    <% end_if %>
    </div>
<% end_loop %>

would be the SS way to do it, so that your display logic doesn't clutter your controller