0

I'm using Kickstrap to theme my bootstrap app and my client wants users to be able to change themes on the fly. I know that to change the theme, all you have to do is change the directory in the themes.less file but I don't know the best way to go about this. Also, I want to remember a user's theme preference so I need to store a cookie to do this, but I'm not entirely sure if it's possible to read cookies straight into LESS as variables.

Basically, I just want a dropdown menu that allows users to select a theme from a list.

Any suggestions would be great! Thanks!

Adam Grant
  • 12,477
  • 10
  • 58
  • 65
samturner
  • 2,213
  • 5
  • 25
  • 31

2 Answers2

0

Assuming you're just using plain HTML/CSS/JavaScript and not a server-side framework, there are a few ways to do this. One involves actually modifying the LESS source code but let me offer some simpler ways:

I would try keeping theme.less blank, so it will just show a default Bootstrap theme.

then you can load your Kickstrap theme on a separate line like this:

<link rel="stylesheet/less" type="text/css" href="kickstrap.less" />
<link rel="stylesheet/less" type="text/css" href="mytheme.less" />
<script src="less.js"></script>

Two theme files

Of course, your theme might have two files, like variable.less and bootswatch.less which may not work keeping them separate:

<link rel="stylesheet/less" type="text/css" href="kickstrap.less" />
<link rel="stylesheet/less" type="text/css" href="variables.less" />
<link rel="stylesheet/less" type="text/css" href="bootswatch.less" />
<script src="less.js"></script>

So instead, you can just @import the variables.less file into bootswatch.less.

@import "variables.less";

And then use the original example from above.

<link rel="stylesheet/less" type="text/css" href="kickstrap.less" />
<link rel="stylesheet/less" type="text/css" href="bootswatch.less" />
<script src="less.js"></script>

By the way, if you're not using LESS client-side, no worries. Just use the compiled CSS files for each instead.

<link rel="stylesheet/less" type="text/css" href="css/kickstrap.css" />
<link rel="stylesheet/less" type="text/css" href="css/bootswatch.css" />

Writing to the DOM

As far as changing this in the DOM, I would try taking advantage of the cssIfy() function already in kickstrap.js. This will take a string (the .css filename) and write it as a stylesheet to the DOM. If you need to write it as a .less file, you can probably just manipulate this function easily. Maybe something like this:

function lessIfy(filePath) {
  var linkElement = document.createElement("link");
  linkElement.setAttribute("rel", "stylesheet/less");
  linkElement.setAttribute("type", "text/css");
  linkElement.setAttribute("href", filePath);
  document.getElementsByTagName('body')[0].appendChild(linkElement);
}

If you do it this way, you'll need to make sure less.js loads afterwards, so try either appending it before that or (worse) loading less.js again after that.

Community
  • 1
  • 1
Adam Grant
  • 12,477
  • 10
  • 58
  • 65
  • Thanks for your answer! I'm still a bit stuck as to how I can use the CSSify function to change and reload the CSS? – samturner Oct 15 '12 at 06:31
  • Are you by chance using a server-side framework like PHP or something? It would make this a looooot easier. – Adam Grant Oct 16 '12 at 16:00
  • I'm not at the moment, but I could easily start using it if it would make something like this possible! – samturner Oct 22 '12 at 11:40
  • That's what I would do. You could just take the bootswatch css files (that's what KS uses) from http://bootswatch.com and load their paths in an array...basically set the .css name in the head as a php variable. – Adam Grant Oct 22 '12 at 19:08
0

Take a look at how the "Included Themes" option changes the themes as you select them.

Helmut Granda
  • 4,565
  • 7
  • 35
  • 51