Jump to:

23461 Posts in 18908 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Different Background

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: 1058 Views
  • swinner
    Avatar
    Community Member
    3 Posts

    Different Background Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Different Background Link to this post

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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Different Background Link to this post

    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>'
             );
          }   
       }   
    }

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Different Background Link to this post

    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.

  • swinner
    Avatar
    Community Member
    3 Posts

    Re: Different Background Link to this post

    thanks for your reply.. it works great

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