user742736
user742736

Reputation: 2729

SilverStripe 3: Alternate value in template within a loop

I am trying to create a page that displays the latest images from the child pages of the holder. Each row will alternate between the example below:

Large Image | Small Image

Small Image | Large Image

Large Image | Small Image

and so on....

template.ss

<div class="row">
<div class="span8">  
  LARGE IMAGE
</div>  
<div class="span4"> 
  SMALL IMAGE
</div>
</div> 
<div class="row">
<div class="span4"> 
  Small Image
</div>  
<div class="span8"> 
 Large IMage
</div> 
</div>  
</div> 
<div class="row">
<div class="span8">  
 Large Image
</div> 
<div class="span4">  
 Small Image
</div> 
</div> 

How can I process that in the template file?

I've tried to write a custom function to process the latest images within the Holder Controller

controller.php

$works = WorkPage::get();

This only returns the image id, I;ve tried a left join but it doesn't return the file path.

$works = WorkPage::get()->leftJoin("File", "\"File\".\"ID\" = \"WorkPage\".\"FeaturedImageID\"");

Upvotes: 0

Views: 1056

Answers (4)

Olli Tyynel&#228;
Olli Tyynel&#228;

Reputation: 576

Just to comment on your join you tested:

this wont work: $works = WorkPage::get()->leftJoin("File", "\"File\".\"ID\" = \"WorkPage\".\"FeaturedImageID\"");

That join doesn't get the data of the joined table; in essence you are requestion workpage objects which dont have that as the data. If you had done the join the otherway around you would be able to get the info you were after

Anyways as Columba allready mentioned you can get the relations correctly on by calling the field as "a function" has one and has many, example $this->hasmanyrelation() < returns the datalist (was that coorect term for ss3 :) ). When using $hasmanyrelation relation on the tempate it just magically gets the collection.

Also you should use the Link() to get the path tho the file in my oppinnion as that works for sitetree objects as well.

Upvotes: 0

drzax
drzax

Reputation: 1665

Your question isn't 100% clear. I'm not sure if you're having trouble with the template looping and conditionals or with getting the image objects from WorkPage so I'll attempt to answer both.

To create the alternating layout, the easiest way is to use a conditional based on whether the loop count is odd or even. A quick untested example:

<% loop $Works %>
<div class="row">
    <% if $Odd %>
    <div class="span8">LARGE IMAGE</div>  
    <div class="span4">SMALL IMAGE</div>
    <% else %>
    <div class="span4">SMALL IMAGE</div>  
    <div class="span8">LARGE IMAGE</div>
    <% end_if %>
</div>    
<% end_loop %>

Documentation reference is at http://docs.silverstripe.org/framework/en/reference/templates#position-indicators

To get different sized images within the loop you can simply use $FeaturedImage->CroppedImage(xxx,xxx). This assumes you have on 'Work' per row and each work has two images, but as I said the question isn't that clear so if my assumptions are incorrect you will need to provide more info about your model and what you're trying to achieve.

Upvotes: 0

user742736
user742736

Reputation: 2729

This is how I did, not sure if it's the best way but it works.

$works = WorkPage::get();

foreach ($works as $work) {
  //Build the IMage Object so we can add it to the Work Object
  $ImageObj = File::get()->byID($work->FeaturedImageID);
  $Image->ID = $ImageObj->ID;
  $Image->Title = $ImageObj->Title;
  $Image->Name = $ImageObj->Name;
  $Image->Filename = $ImageObj->Filename;
  $work->ImageObj = $Image;

  $ImagePath = $work->ImageObj->Filename;

}

Upvotes: 0

jfbarrois
jfbarrois

Reputation: 989

File::get()->
    leftJoin("SiteTree", "\"SiteTree\".\"ParentID\" = ".$this->ID)->
    leftJoin("WorkPage", "\"WorkPage\".\"ID\" = \"SiteTree\".\"ID\"")->
    where("\"File\".\"ID\" = \"WorkPage\".\"FeaturedImageID\"");

might be the sql query you are after (untested though)

Upvotes: 0

Related Questions