Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » show blocks on certain pages

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1122 Views
  • Kai
    Avatar
    Community Member
    4 Posts

    show blocks on certain pages Link to this post

    I am new to SilverStripe, and made my first template - and I am having problems understanding how to display certain blocks (text or image) on only a few pages. I would like to show a different image (inside my template) on 2 pages

    Is it possible to add a text block (like a note) on all pages? It is very important that the user can update the text inside SilverStripe CMS.

    It would be great if someone could give me more information about this

  • Mrfixer
    Avatar
    Community Member
    49 Posts

    Re: show blocks on certain pages Link to this post

    Been there myself the last couple of days, this thread should point you in the right direction if i understand you correctly http://www.silverstripe.org/general-questions/show/5651 or if not then i aint understood what your trying to achieve..

    My problem was i needed differing content throughout the sidebars (images and titles) on every page, eventually i managed to find a way to add these blocks into the ACP (almost idiot proof from a web site managers point of view) and can update per page differing content, im not too good at explaining, but if you want the files i knocked up with the code then just holler via pm and i will pass em on to you..

  • Kai
    Avatar
    Community Member
    4 Posts

    Re: show blocks on certain pages Link to this post

    Hello,
    if you have some sourcecode that would be nice.

    I still need to learn more about the Sapphire framework, and how this works.

  • Mrfixer
    Avatar
    Community Member
    49 Posts

    Re: show blocks on certain pages Link to this post

    This should help if i understand correctly, what im doing is adding two textfields and two image uploading fields into the CMS (this code applies to my Left and Right Sidebars), my sidebars are just includes from SidebarLeft.ss and SidebarRight.ss, when these fields have content added via the CMS i want that content to be different across the pages.

    firstly extend your pages relevent .php code in "mysite/code": (i extended my existing Page.ss so the file i added the code to is Page.php, this takes it across every page on my site, if you dont want that then you could use something like this http://www.silverstripe.org/template-questions/show/15692 or set up a page template just to use for the pages that you want to include the blocks)

    <?php
    class Page extends SiteTree {

    static $db = array(
    'HtagSbLone' => 'Text', // left sidebar.
    'HtagSbLtwo' => 'Text',
    'HtagSbRone' => 'Text', // right sidebar.
    'HtagSbRtwo' => 'Text',
    );
    static $has_one = array(
    "ImageSbLftone" => "Image", // left sidebar
    "ImageSbLfttwo" => "Image",
    "ImageSbRgtone" => "Image", //right sidebar
    "ImageSbRgttwo" => "Image",
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.SidebarLeft", new TextField( "HtagSbLone", 'Header Tag One'));
    $fields->addFieldToTab("Root.Content.SidebarLeft", new ImageField("ImageSbLftone", "Image One"));
    $fields->addFieldToTab("Root.Content.SidebarLeft", new TextField( "HtagSbLtwo", 'Header Tag Two'));
    $fields->addFieldToTab("Root.Content.SidebarLeft", new ImageField("ImageSbLfttwo", "Image Two"));
    $fields->addFieldToTab("Root.Content.SidebarRight", new TextField( "HtagSbRone", 'Header Tag One'));
    $fields->addFieldToTab("Root.Content.SidebarRight", new ImageField("ImageSbRgtone", "Image One"));
    $fields->addFieldToTab("Root.Content.SidebarRight", new TextField( "HtagSbRtwo", 'Header Tag Two'));
    $fields->addFieldToTab("Root.Content.SidebarRight", new ImageField("ImageSbRgttwo", "Image Two"));
    return $fields;
    }
    }
    ?>
    note this in the last block of code "Root.Content.SidebarLeft" this instructs that a new tab be created in the CMS called Sidebar Left this tab will then contain 2 text fields and two image uploading fields, Root.Content.SidebarRight is the same instruction but this creates my Right Sidebar tab,

    all left to do then is just echo the variable out in its relevant place on its relevent page,

    im new to this myself but if anybody can suggest another way then im all ears..

  • dgalperin
    Avatar
    Community Member
    4 Posts

    Re: show blocks on certain pages Link to this post

    I am really new to this too, but here is what I did to have blocks only on certain pages.

    <% if Title = pagetitle %>
    <% else %>
    <div class="yourblock">
    </div>
    <% end_if %>

    pagetitle is the title of the page you don't want the block on.

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: show blocks on certain pages Link to this post

    - if you want to be able to have a different textblocks on each page, then Mrfixer is showing a great way to do it.

    - If you want the same bit of text to apear on each page, and still have it user-editable in the CMS - like a footer or such - this might be something for the site config

    - you could combine the two if you want an editable default text for each block (site config) and the opportunity to change it for certain pages

  • WebInt
    Avatar
    Community Member
    11 Posts

    Re: show blocks on certain pages Link to this post

    The thing is the combination still doesn't seem to give particularly good control over your content areas, when you have an increasing number of pages.

    Say you have 100 pages, and you have a block of content you want to appear on every page but one (so it can't be a static content block), you will have a real job ahead of you copying and pasting your content into each sidebar content area, and again if you want to change it.

    What seems to be the best theoretical method is to be able to choose which pages a single content block should appear on, per content block, so you change the content in one place, and it applies to everywhere it appears.

    So say for a content block called "Contact Details", you have a tab called "Appears On". Here you are given a sitetree with checkboxes next to each page, and you select your pages with extra options like:

    • Appear on all pages except.
    or
    • Appear on selected pages.

    This seems to be a huge amount of work, but would be a great addition to any site.

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