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.

Archive

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

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

Loading other page content into a sidebar content area?


Reply

8 Posts   4659 Views

Avatar
AK Mac

10 July 2008 at 3:58am (Last edited: 10 July 2008 3:59am), Community Member, 4 Posts

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

10 July 2008 at 9:30am (Last edited: 10 July 2008 9:36am), Community Member, 712 Posts

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

10 July 2008 at 9:40am Community Member, 501 Posts

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

10 July 2008 at 9:54am (Last edited: 10 July 2008 9:54am), Community Member, 712 Posts

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

10 July 2008 at 1:50pm Community Member, 4 Posts

Thanks guys!

I will try this right away!

-Aaron

Avatar
AK Mac

10 July 2008 at 2:18pm Community Member, 4 Posts

Hey Hamish,

Your method worked great!

This is HUGE! Thank you!

-Aaron

Avatar
famous

11 July 2008 at 11:10am Community Member, 14 Posts

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

Avatar
bird

5 September 2008 at 12:41pm Community Member, 9 Posts

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