I would like to make a Rails app that lets users enter data and then allow them to change the page’s theme. This way their data could be styled differently based on the theme they choose. How would I go about doing this?
- Change stylesheet?
- Two separate views with different classes/elements?
- Dynamically just change classes/ids/selectors?
- ?
Thanks
The easiest way to theme a site is to simply link to a different stylesheet. You can do this dynamically using something like:
Then you can have a couple manifests for themes. For example, your assets directory can look something like this:
This will get you started with pure css theming. At some point you’ll probably want to also them javascript and html layouts. When you start finding the need to do something like this in your html:
then it’s time to implement a more robust theming framework:
render_themed_partialNote: This is all for static themes. For dynamic themes (e.g. where an admin can login and edit the stylesheets or html), you’ll have to store theming information in the database. Depending on your architecture, you may be able to provide a set of static themes, and then another theme that dynamically grabs styling data from the database. At that point, you’re developing a CMS, however, so it’s outside the scope of this answer 🙂