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.

General Questions /

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

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

SS3 Images from DataList


Go to End
Reply


3 Posts   746 Views

Avatar
quanto

Community Member, 81 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, 1191 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, 81 Posts

16 July 2012 at 8:19pm

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