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.

Customising the CMS

allow user to customize page theme


Go to End
Reply

21 Posts   4400 Views

Avatar
mccarville

17 January 2009 at 11:31am Community Member, 32 Posts

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

17 January 2009 at 11:44am Community Member, 712 Posts

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

17 January 2009 at 12:06pm Community Member, 32 Posts

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

19 January 2009 at 7:37am Community Member, 501 Posts

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

19 January 2009 at 1:00pm Community Member, 32 Posts

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

19 January 2009 at 1:51pm Community Member, 244 Posts

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

20 January 2009 at 10:25am Community Member, 32 Posts

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

20 January 2009 at 10:36am Community Member, 501 Posts

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