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.

Template Questions /

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

show blocks on certain pages

Go to End

7 Posts   1794 Views


Community Member, 4 Posts

3 February 2011 at 6:49am

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


Community Member, 49 Posts

3 February 2011 at 1:13pm

Been there myself the last couple of days, this thread should point you in the right direction if i understand you correctly 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..


Community Member, 4 Posts

3 February 2011 at 6:06pm

if you have some sourcecode that would be nice.

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


Community Member, 49 Posts

4 February 2011 at 6:40am

Edited: 04/02/2011 11:18am

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 and, 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 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 or set up a page template just to use for the pages that you want to include the blocks)

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


Community Member, 4 Posts

28 February 2011 at 7:13am

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

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


Forum Moderator, 1377 Posts

28 February 2011 at 8:52am

- 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


Community Member, 14 Posts

12 March 2011 at 3:44am

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.
• Appear on selected pages.

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