SilverStripe 3: Alternate value in template within a loop

风格不统一 提交于 2019-12-12 03:35:59

问题


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\"");

回答1:


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)




回答2:


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;

}



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/15098248/silverstripe-3-alternate-value-in-template-within-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!