Access theme settings in renderers or scripts?

Access theme settings in renderers or scripts?

by David Scotson -
Number of replies: 5
Can you access the current theme settings in renderers and/or command line scripts?

I'm trying to 'compile' a theme by calling a PHP script from the command line to generate the various CSS files. I'd like the output to depend on the current settings for a particular theme that have been set via the web UI.

Has anyone done anything like this before, I'm not really sure where to start?

Also, if you wanted to turn a renderer on/off (or vary it's output) based on theme settings, what would that look like?
Average of ratings: -
In reply to David Scotson

Re: Access theme settings in renderers or scripts?

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

This sounds a bit vague. Have you got a flowchart to show the processes needed? Even if it is only a rough idea.

To get at the data stored in the DB you would need to read mdl_config_plugins to get the UI custom CSS settings for the theme in question IF it exists.

When you say generate some CSS files, how are you going to do that? Would you have a form that one fills out with the requirments based on a predefined template, to select colors/images etc...?  Something like...

http://www.thematoweb.com/

Mary

In reply to Mary Evans

Re: Access theme settings in renderers or scripts?

by David Scotson -
Yes, something like that. The idea is that I have one flexible theme, with a standard settings page which lets you select various options e.g. responsive or not, icon font/glyphicons/new moodle icons and then displays them to you when you save so you can test it out on a live site.

However, you don't want all that baggage in your final theme, so I'd like something automated (currently a command line script, but ideally you'd click a link in the Moodle and get a zipped theme) that takes your decisions and makes them permanent, and drops all the unnecessary stuff.

My flexible theme uses LESS, so you can choose a base color and it'll calculate different shades and gradients based on that color etc. Currently that compilation step is done in the CSS Processor and I have a separate file that calls the same function I call from the CSS Processor and returns the output CSS directly and I write it to a file as part of my theme building script. Currently I have to manually specify the various settings again in PHP, which isn't exactly a burden for me, but I'd like others to be able to use the same system and help me find bugs so if it could pick up the settings from Moodle that would be great.
In reply to David Scotson

Re: Access theme settings in renderers or scripts?

by Danny Wahl -

As to part one, seems all you would need to do is call "global $PAGE" to get access to the settings ($PAGE->theme->setting->settingname) but you might need to give it a page context/definition, which might be hard.

As to part 2, I'm not sure if renderers can also call page (it should since lib can...) but if it can then you could call conditionals inside the class (or maybe outside of it?) like:

class theme_renderer extends core_renderer {

global $PAGE;

function do_something_for_yesno($value) {

$yesno = $PAGE->theme->setting->yesno;

If ( $yesno == 'yes' ) {

$value = 'it says yes!';

} else {

$value = 'it says no...';}

return $value

}

}

Average of ratings: Useful (1)
In reply to Danny Wahl

Re: Access theme settings in renderers or scripts?

by Paul Nicholls -

Renderers (at least your theme's core renderer (i.e. class theme_yourtheme_core_renderer extends core_renderer) - I'm not 100% sure on other renderers, as I haven't tried) can access the page (and therefore the theme settings) via $this->page - so you don't need to bother with "global $PAGE" etc, just use $this->page->theme->settings->yourthemesettingname!

As for turning a renderer on/off based on a setting, I guess you could just have the following at the top of your renderer function:

public function renderfunc($arg1, $arg2) {
    if ($this->page->theme->settings->relevantsetting) {
        return parent::renderfunc($arg1, $arg2);
    }
    [your custom code here]
return $content;
}

That'll make it use the standard renderer if your theme setting "relevantsetting" is truthy, or your custom renderer if it's falsey - so it'd act as a "use core renderer" flag; you may therefore need to change the condition in order to get the behaviour you seek.

 

HTH,
Paul

Average of ratings: Useful (1)
In reply to David Scotson

Re: Access theme settings in renderers or scripts?

by Mauno Korpelainen -

David,

you might find code of splash theme useful... check how colourswitcher (implemented by Sam H.) there works.

Usually this kind of things are done with sessions but in Splash theme layout files (for example general.php) are using yui based colourswitcher.js and lib.php is using set_user_preference and get_user_preferences to sent selected settings to database (user_preferences) and back. Chosen userpreference is sent to body classes array with

$bodyclasses[] = 'splash-'.splash_get_colour();

and these body classes are joined to body tag some rows later. Finally you need of course css for different body classes in theme files.

I have been using the same idea for editor switchers, theme (preset) switchers and font switchers.