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.

We've moved the forum!

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.

Archive /

Our old forums are still available as a read-only archive.

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

Loading other page content into a sidebar content area?


Go to End


8 Posts   5736 Views

Avatar
AK Mac

Community Member, 4 Posts

10 July 2008 at 3:58am

Edited: 10/07/2008 3:59am

I have my main content area and I also have two sidebar content areas. I want to know if I can load another pages content (reuse) into one or both of the sidebars using TreeDropdownField?

Right now I am trying this,

<?php

class Page extends SiteTree {
static $db = array(

'LeftSide' => 'HTMLText',
'RightSide' => 'HTMLText'
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new TreeDropdownField("LeftSide", "Choose a page to appear in the left sidebar:", "SiteTree"));
$fields->addFieldToTab('Root.Content.Main', new TreeDropdownField("RightSide", "Choose a page to appear in the right sidebar:", "SiteTree"));
return $fields;
}
static $has_one = array(
);
}

This just returns the page id in the sidebar instead of the actual page HTML.

Am I way off? Is this even possible.

Thanks in advance!

-Aaron

Avatar
Hamish

Community Member, 712 Posts

10 July 2008 at 9:30am

Edited: 10/07/2008 9:36am

First of all - a test - I just wrote a long response and the forum totally bailed on submission...

...ok that really sucks. Here goes again:

Store those numbers as Ints - we'll use them later:

class Page extends SiteTree { 
   static $db = array( 
    
   'LeftSide' => 'Int', 
   'RightSide' => 'Int' 
   ); 
}

Now you can create a couple of customer getters to retrieve the pages you want:

class Page extends SiteTree { 
   ...
   function RightPage() {
      return DataObject::get_by_id("Page", $this->RightSide);
   }
   function LeftPage() {
      return DataObject::get_by_id("Page", $this->LeftSide);
   }
}

Now, within your side bar .SS template files (or wherever you want, really) you can control those pages:

<% control RightPage %>
<h3>$Title</h3>
$Content
<% end_control %>

This is totally untested, but it should get you on the right track!

Avatar
dio5

Community Member, 501 Posts

10 July 2008 at 9:40am

Why not store those numbers as has_one's?

static $has_one = array(
'LeftSide' => 'Page',
'RightSide' => 'Page'
);

Then, in the template you can do:

<% control LeftSide %>
<h3>$Title</h3>
$Content
<% end_control %> 

<% control RightSide %>
<h3>$Title</h3>
$Content
<% end_control %> 

without the need to make those extra getter methods.. I think :)

Avatar
Hamish

Community Member, 712 Posts

10 July 2008 at 9:54am

Edited: 10/07/2008 9:54am

:-0

Sometimes I forget how easy Silverstripe makes this sort of thing sometimes.

For my own peace of mind, the two are functionally equivalent though, right? The has_one relationship will store the page ID and get the dataobject automatically?

Avatar
AK Mac

Community Member, 4 Posts

10 July 2008 at 1:50pm

Thanks guys!

I will try this right away!

-Aaron

Avatar
AK Mac

Community Member, 4 Posts

10 July 2008 at 2:18pm

Hey Hamish,

Your method worked great!

This is HUGE! Thank you!

-Aaron

Avatar
famous

Community Member, 14 Posts

11 July 2008 at 11:10am

ok I am confused
would I need to download tree control to understand a bit better
what you guys are talking about?

Avatar
bird

Community Member, 9 Posts

5 September 2008 at 12:41pm

Hi there!

I logically understand what you are talking about but pratically I can't realize it on my own...
Where to pass these code-snippets and how to set the page I want to display in the sidebar!?

I passed the code in mysite/code/Page.php - is this the right place?
Where to set that the page thaht should be displayed e.g. "id=contact"?

I'm new to ss and php so please be patient...
Thanks Berny