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

allow user to customize page theme


Go to End


21 Posts   7064 Views

Avatar
mccarville

Community Member, 32 Posts

17 January 2009 at 11:31am

I'm by no means any sort of expert when it come to coding or php but I am trying to implement a drop down box in the cms on Root.content.main that will allow the user to set the theme for the page.

I could but completely off but this is the code I have come up with so far... as now I getting a parse error on line "SSViewer::set_theme('$Theme');"

---------------------------
<?php

class Page extends SiteTree {

static $db = array(
'Theme' => "Enum('higherground,blackcandy','higherground')"
);

static $has_one = array(
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DropdownField(
'Theme',
'Select A Theme:',
singleton('Page')->dbObject('Theme')->enumValues()
));
return $fields;}
}

class Page_Controller extends ContentController {

function init() {

switch($this->URLSegment) {

SSViewer::set_theme('$Theme');
break;
}

parent::init();

Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");

}

}

?>

P.S. LOVE SilveStripe!!!

Avatar
Hamish

Community Member, 712 Posts

17 January 2009 at 11:44am

Change:

SSViewer::set_theme('$Theme');

to:

SSViewer::set_theme("$Theme");

A string encapsulated in single quote marks will not evaluation php variables, so your script was trying to set it to a theme called $Theme

Avatar
mccarville

Community Member, 32 Posts

17 January 2009 at 12:06pm

I tried what you suggested and I still got a parse error on the same line. Just for kicks I ran the code below and it worked as anticipated. Which would lead me to believe it is definitely in the commented function... but I don't have any idea how to fix it...

<?php



class Page extends SiteTree {

static $db = array(
'Theme' => "Enum('higherground,blackcandy','higherground')"
);

static $has_one = array(
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new DropdownField(
'Theme',
'Select A Theme:',
singleton('Page')->dbObject('Theme')->enumValues()
));

if (!Permission::check("ADMIN")){
   $fields->removeFieldFromTab('Root','Access');
} 
return $fields;}
}

class Page_Controller extends ContentController {

function init() {

/*switch($this->URLSegment) {

SSViewer::set_theme("$Theme");
break;
}*/

parent::init();

Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");

}

}

?> 

Avatar
dio5

Community Member, 501 Posts

19 January 2009 at 7:37am

Maybe I'm missing something, but why even put quotes around $Theme?

SSViewer::set_theme($Theme); should work as well.

Also,

why have a switch when you don't have 'cases'?

Just leave the switch out I would think. If $theme is set per page it will automatically set this, no switch $urlsegment needed...

Avatar
mccarville

Community Member, 32 Posts

19 January 2009 at 1:00pm

Tried what you suggested with no luck... I tried it with no quot, single quote, double quotes, with and without the swith $URLsegment... nothing

I also tried removing the theme call in the _config file but that did not help

I was able to build and seemingly select the theme in the CMS but the pages would not render stating an error in SSVeiwer.php on line 151

Avatar
ajshort

Community Member, 244 Posts

19 January 2009 at 1:51pm

Hey there,

remove the switch statement, all you need is to put "SSViewer::set_theme($this->Theme);" where you have the commented out switch.

Avatar
mccarville

Community Member, 32 Posts

20 January 2009 at 10:25am

Tried what you suggested and I am still getting errors. Again the build is going well but when the page tries to render i get errors... I also see the same errors if I try to access the admin page ... The error is saying that it can not find the template Page.ss... Almost seems like it is looking for the for the .ss files before the theme is set??

Avatar
dio5

Community Member, 501 Posts

20 January 2009 at 10:36am

Could you check a few things:

- try a flush first (add ?flush=1 to your url)
- check if there's really a value saved in the database?
- do a Debug::show($Theme); right before SSViewer::set_theme... to see what the value is.

Perhaps this call to set the theme is indeed too late and should happen earlier?

Go to Top