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.

Customising the CMS /

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

Sidebar content/editable regions help (please)


Go to End


12 Posts   5618 Views

Avatar
AlaVive

Community Member, 42 Posts

28 July 2009 at 7:59am

Edited: 28/07/2009 9:17am

I really have learned quite a bit from these forums, and from the great help at http://www.ssbits.com/.

However, I am having a really difficult time getting static side content to appear in my pages.

In the home page, the user needs to be able to modify the html for 3-4 varied content areas. One of these areas is the sidebar; the other 3 areas are locations where, if he has access to the html editor, he can use his limited html skills to enter other ads or whatever information he likes.

The rest of the pages are to default to the sidebar navigation that seems to be working fine.

The following is the code I've tried. Is there anyone out there who can help me with this?

(I'm using v2.3.2...)
___________________

StaticSidebar.php

<?php 

class StaticSidebar extends HomePage {

static $db = array(
'SidebarHTML' => 'HTMLText'
);

static $has_one = array(
);

function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab("Root.Content.Useme", new HtmlEditorField( "SidebarHTML", "HTML Editor")); 

return $fields;
}

}

class StaticSidebar_Controller extends HomePage_Controller{

}
?>

This successfully adds it to the CMS, giving me the option to create a new "Static Page."
___________________
HomePage.php (I also tried this in the Page.php to no avail)

<?php
/**
 * Defines the GA HomePage page type
 */
 
class HomePage extends Page {
   	static $db = array(
	);
	
   static $has_one = array(
   );
   
 
}


class HomePage_Controller extends Page_Controller { 
public function GetStaticSidebar(){
		return DataObject::get_one("StaticSidebar");
}
?>

___________________
StaticSidebar.ss

<% control GetStaticSidebar %>

$SidebarHTML

<% end_control %>

___________________
In my HomePage.ss layout, I call it all this way:

<div id="sidebar">
	<% include StaticSidebar %>
</div>	

I get no errors. The sidebar just fails to appear in the draft/published page. I'm thinking it's pretty obvious to someone who hasn't looked at this for days.

I'm willing to start over from scratch if I can just get something that will work. I have to do this for another project in a few days, as well. And I'm a bit nervous that it's taking me this long in an effort to save the client time!

Any help is GREATLY appreciated!

Avatar
Willr

Forum Moderator, 5523 Posts

28 July 2009 at 9:24pm

I am not sure how you are doing it is the best way - do you really want a subclass of the homepage for the sidebar? If the sidebar content is for the homepage then you should just add a field to the homepage. Then calling that CMS Field is simple.

Also Watch out for the use of Get in your template functions. Some times this can cause issues. I would change your GetStaticSidebar to StaticSidebarContent() for now just to be safe - also note that is different to your include name, if they are the same you could run into trouble as well.

Avatar
AlaVive

Community Member, 42 Posts

1 August 2009 at 2:05pm

Thanks, willr. I did, indeed, over-complicate the matter in my state of hurried panic. The simple solution worked.

Avatar
mhdesign

Community Member, 216 Posts

19 November 2009 at 8:44pm

Edited: 19/11/2009 8:45pm

I am also trying to add an editable sidebar to the right hand side of my homepage. I see that on this post Willr said:

“If the sidebar content is for the homepage then you should just add a field to the homepage. Then calling that CMS Field is simple.”

Can you please clarify? Do you mean simply add an include to the HomePage.ss file?

Almost finished on this one - this is the final hurdle!!

Avatar
Willr

Forum Moderator, 5523 Posts

19 November 2009 at 8:50pm

static $db = array( 
'SidebarHTML' => 'HTMLText' 
);

To include the that field you just need to put $SidebarHTML in your template.

Avatar
mhdesign

Community Member, 216 Posts

20 November 2009 at 2:08pm

Sorry Will, I’m afraid you have a SilverStripe newbie here (as if you hadn’t guessed)...

So can you please clarify - where am I to put this code so I can access sidebar from CMS?

static $db = array(
'SidebarHTML' => 'HTMLText'
);

Thanks again!

Avatar
Willr

Forum Moderator, 5523 Posts

23 November 2009 at 7:16pm

arthurdent - if you haven't already I recommend reading the tutorials. Tutorial 2 covers adding fields to the cms.

So in this case do you want to add a sidebar area thats different on various pages or the same through out the site? The easiest way is to put that code 'SidebarHTML' => 'HTMLText' inside the $db array that is in the 'mysite/code/Page.php' file. Then like AlaVives code you need to add it to the CMS using the following code which also goes in that Page.php

function getCMSFields() { 
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Sidebar", new HtmlEditorField( "SidebarHTML"));
return $fields; 
}

Then in your template (say for example the themes/blackcandy/templates/Page.ss file) you can call that new CMS field by adding $SidebarHTML

The code for your page.php file should look something like this

/mysite/code/Page.php

<?php

class Page extends SiteTree {

static $db = array( 
'SidebarHTML' => 'HTMLText' 
);

static $has_one = array( 
);

function getCMSFields() { 
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Sidebar", new HtmlEditorField( "SidebarHTML"));
return $fields; 

}

}

class Page_Controller extends ContentController {

} 
?>

Avatar
mhdesign

Community Member, 216 Posts

23 November 2009 at 9:42pm

Thanks heaps for this Will -- I'm wanting a custom sidebar on the HomePage file, not the entire site. I take it in this case the file I am working with is "mysite/code/HomePage.php"?

Appreciate your comment about tutorials -- I have been through them, both on the SilverStripe forum and elsewhere, but I really had to hit the ground running on this project -- a month ago I'd never even heard of SilverStripe! While the tutorials are great, sometimes you just can't see the wood for the trees, as it were. Set out as you have presented it, I'm starting to get a clearer idea of how the different parts of the CMS and the templates interact. When I first looked at all the various files that came with a SS install it was a bit daunting -- while I've learned a lot about various file types and operating systems over the years and want to learn all that I can, initially I'm from a design background!

That said, I'm really impressed with SS and the way it can be implemented into custom designs, so I'm really keen to get to grips with it!!

Go to Top