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, simon_w

Different Background


5 Posts   1071 Views


24 December 2010 at 12:05pm Community Member, 3 Posts


is there a possibility to get a different background on each page?
page1 = background_image01.jpg
page2 = background_image02.jpg
page3 = background_image03.jpg

thank you


24 December 2010 at 10:01pm Forum Moderator, 1796 Posts

If the pages are different types then...

within the template (*.ss)

<% Classname = PageTypeOne %> background_image01.jpg <% end_if %>
<% Classname = PageTypeTwo %> background_image02.jpg <% end_if %>

or by the URLSegment...

<% URLSegment= pageone %> background_image01.jpg <% end_if %>
<% URLSegment= pagetwo %> background_image02.jpg <% end_if %>


28 December 2010 at 7:03am Community Member, 271 Posts

Or try something like this to add a bgimage to each page:

class Page extends SiteTree {
   static $has_one = array(
      'BackgroundImage'   =>   'Image'
   function getCMSFields(){
      $fields = parent::getCMSFIelds();
      $bgImage= new FileIFrameField("BackgroundImage", "BackgroundImage");
      $fields->addFieldToTab("Root.Content.BackgroundImage", $bgImage);
      return $fields;

class Page_Controller extends ContentController {

   public function init() {
         Requirements::insertHeadTags('<style type="text/css">html{ background: url('.$this->BackgroundImage()->FileName.') no-repeat 50% 0;}</style>'


29 December 2010 at 8:46pm Community Member, 395 Posts

if would recommend @Martijn's solution,

but if you want some random background image for any page without checking the page type I'd rather go with a decorator to the SiteConfig.php



class BackgroundDecorator extends DataObjectDecorator{

public function extraStatics(){
return array(
"Images" => "BackgroundDecorator_Image"

public function updateCMSFields($fields){
$fields->addFieldToTab("Root.Backgrounds", new TableField($this, "Images", "BackgroundDecorator_Image", array("Title" => "Title")));

public function getRandomImage($fields){
return DataObject::get_one("BackgroundDecorator_Image", "SiteConfig = {$this->owner->ID}", false, "RAND()");


class BackgroundDecorator_Image extends DataObject{

static $db = array(
"Title" => "Varchar"

static $has_one = array(
"Image" => "Image",
"SiteConfig" => "SiteConfig"


so you will be able to use $SiteConfig.getRandomImage.Image from your templates.


29 December 2010 at 10:51pm Community Member, 3 Posts

thanks for your reply.. it works great