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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS3 Images from DataList


Go to End


3 Posts   960 Views

Avatar
quanto

Community Member, 91 Posts

14 July 2012 at 12:58am

I'm trying to get several images from a DataList into a viewer. However, the function returns the values inside the 'File' table of SilverStripe.

my code:
FotoPagina.php

class FotoPagina extends Page {
...

public static $has_many = array(
    'MyFotos'=>'FotoItem'
);
...
}
class FotoPagina_Controller extends Page_Controller {
...
   
  function getFotos(){
    $list =  DataList::create('FotoItem')->relation('Foto')->where('FotoItemID = '.$_GET['id']);
    return $list;
  }
...
}

FotoItem.php
class FotoItem extends DataObject {
...
public static $many_many = array(
     'Foto'=>'Image'
  );
...
}

Page.ss
<% loop getFotos %>
   <li>  
     <a class="thumb" name="optionalCustomIdentifier $ID" href="$Filename">
         <img src="$Filename"/>
     </a>
   </li>
<% end_loop %>

SS will output the $ID and the Filename, but I need to get the image object, so I can set the $Image.setWidth(140) option. Is there a way to get this with the DataList, or is there any other option?

Avatar
martimiz

Forum Moderator, 1391 Posts

14 July 2012 at 2:29am

Within the <% loop %> structure you are within the scope of the Image. So theoretically you should be able to just do $setWidth(140) without the Image. preset. Haven't tried this though...

Avatar
quanto

Community Member, 91 Posts

16 July 2012 at 8:19pm

Thanks! It works. Never thought it would be that easy...