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
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
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
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 %>
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>'
);
}
}
}
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.
thanks for your reply.. it works great