Adding settings to a DecentCMS theme
Philip asked an interesting question on the DecentCMS forums: “are there plans to allow for theme settings”? That is a great question, and I’m happy to report that the system already allows for it, that I was able to verify that it works, and that it was ridiculously easy.
In DecentCMS, themes are just modules that are under the “themes” directory. The convention is that they should only contain “theme-y” things, such as template overrides, stylesheets, icons, etc., but essentially they are modules (and modules could in principle do theme-y things, as themes can do module-y things). One of the things a module can do is expose features, and those features can have settings that can be defined at the level of the site.
The first thing we’ll want to do, then, is to define a feature on our theme: open the theme’s “package.json” file and add the following section…
"features": {
"default-theme-settings": {
"name": "Default theme settings",
"description": "Settings for the default theme"
}
},
In the “sites/[name-of-the-site]/settings.json” file for your site, under the features section, add the following settings definition (I’m only defining a background color here, but you can define any number of settings you want, and they don’t have to be limited to string values):
"default-theme-settings": {
"background-color": "#c0c0c0"
},
Finally, edit any of the templates under the “views” folder of your theme (I’m changing “themes/default/views/layout.tl” here), and add code that uses the settings we added, that can be found under the “site.features.default-theme-settings” object:
<body role="document"
style="background-color: {site.features.default-theme-settings.background-color};">
And that’s it.