Jump to:

23376 Posts in 18176 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Get Random Image

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 2
Go to End
Author Topic: 1194 Views
  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Get Random Image Link to this post

    You don't need to extend the Page_Controller. Just put it in your SiteConfigCustom class

    class SiteConfigCustom extends DataExtension {

    public function getRandomImage() {
    return $this->BackgroundImages()->sort('RAND()')->First();
    }

    }

    and as far as i know, in your template file (Page.ss) you have to put the $ sign when using <% loop %>

    <% loop $SiteConfig %>

    <% end_loop %>

    It may not work without the $.

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    If I put it up there, it breaks and I get the blank white page of death.

    I don't believe the $ is necessary unless your doing something like <% if $MyDinner="kipper" %>

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Get Random Image Link to this post

    oh, made a silly mistake...it should be

    public function getRandomImage() {
    return $this->owner->BackgroundImages()->sort('RAND()')->First();
    }

    need the "owner" part when your in a DataExtension . I think that might fix it.

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    That seems to work with this change to the template:

    Page.ss

    <% loop SiteConfig %>
       $RandomImage.BackgroundImage
    <% end_loop %>

    Thanks to all!

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Get Random Image Link to this post

    oh that's right. Good call on the template.

    you could also use

    <% loop SiteConfig.RandomImage %>
    $BackgroundImage
    <% end_loop %>

    if you wanted to loop through more then one random image.

  • socks
    Avatar
    Community Member
    190 Posts

    Re: Get Random Image Link to this post

    Ok cool, thanks again!

    1194 Views
Page: 1 2
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.