Jump to:

23480 Posts in 18951 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » SS3 Images from DataList

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

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

Page: 1
Go to End
Author Topic: 617 Views
  • quanto
    Avatar
    Community Member
    81 Posts

    SS3 Images from DataList Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: SS3 Images from DataList Link to this post

    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...

  • quanto
    Avatar
    Community Member
    81 Posts

    Re: SS3 Images from DataList Link to this post

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

    617 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.