Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Loading other page content into a sidebar content area?

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

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

Page: 1
Go to End
Author Topic: 4611 Views
  • AK Mac
    Avatar
    Community Member
    4 Posts

    Loading other page content into a sidebar content area? Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

    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!

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

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

  • AK Mac
    Avatar
    Community Member
    4 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

    Thanks guys!

    I will try this right away!

    -Aaron

  • AK Mac
    Avatar
    Community Member
    4 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

    Hey Hamish,

    Your method worked great!

    This is HUGE! Thank you!

    -Aaron

  • famous
    Avatar
    Community Member
    14 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

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

  • bird
    Avatar
    Community Member
    9 Posts

    Re: Loading other page content into a sidebar content area? Link to this post

    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

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