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

Different Background


Go to End
Reply


5 Posts   1312 Views

Avatar
swinner

Community Member, 3 Posts

24 December 2010 at 12:05pm

hello

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

Avatar
swaiba

Forum Moderator, 1837 Posts

24 December 2010 at 10:01pm

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

Avatar
Martijn

Community Member, 271 Posts

28 December 2010 at 7:03am

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

<?php
class Page extends SiteTree {

static $has_one = array(
'BackgroundImage' => 'Image'
);

function getCMSFields(){
$fields = parent::getCMSFIelds();
$bgImage= new FileIFrameField("BackgroundImage", "BackgroundImage");
$bgImage->setFolderName('backgrounds');
$fields->addFieldToTab("Root.Content.BackgroundImage", $bgImage);

return $fields;
}
}

class Page_Controller extends ContentController {

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

Avatar
Nivanka

Community Member, 400 Posts

29 December 2010 at 8:46pm

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

eg

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

Avatar
swinner

Community Member, 3 Posts

29 December 2010 at 10:51pm

thanks for your reply.. it works great