Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-02-01 14:05:10

ubernostrum
Member
From: Lawrence, KS
Registered: 2004-05-05
Posts: 238
Website

A non-plugin style switcher

I’ve been wanting to sit down and flesh out a full-featured style switcher for Textpattern for a while now (I looked at this one but it’s not quite what I wanted, and I’ve never cared for the ALA JavaScript switcher), but with the imminence of the 1.0 release I’m wary of doing it as a plugin or relying on TXP’s built-in CSS system. So I’ve put together a nice little script that can work independently of Textpattern, and can be set up with very little fuss.

The script is available for download here, and once you’ve got it you’ll need to do three things to get it working:

First, unzip and open up the script (when you unzip the file you should get a script called ‘styles.php’). There’s a line in it which looks like this (line #23 to be exact):

$site_domain = '.' . 'example.com';

Change ‘example.com’ to whatever your domain is (do not put an ‘http://’ in there). For example, on my site I’ve changed it to

$site_domain = '.' . 'shtuff.us';

You can put a ‘www’ in your domain name here if you want to, but it’s not necessary and actually might cause some problems.

Next you’ll need to do one of two things. You can either take the stylesheet you want to have be your ‘default’ (the one people will get if they haven’t chosen a style through the switcher) and rename it ‘default.css’, or you can find this line (line #39) in the script:

$style = $_COOKIE['stylesheet'] ? $_COOKIE['stylesheet'] : 'default';

And replace it with the name of your stylesheet. So if you had a stylesheet called, say, ‘main.css’, you’d just replace ‘default’ with ‘main’. And then you can upload the script, because you don’t need to make any more changes to it.

Finally, go into your page templates and change any link and/or style tags you’re using on your pages to point to this script. So you’d want something like this:

<link rel="stylesheet" href="styles.php" type="text/css" />

At this point all you’ve really done is replaced Textpattern’s css.php with my styles.php, but now it’s very easy to do style switching. Say you have an alternate layout you want people to be able to choose, and it’s in a stylesheet called ‘othertheme.css’. Here’s what you do:

  1. Upload that stylesheet.
  2. Somewhere on one of your pages put a link like this: <a href="styles.php?style=othertheme">click here to change theme</a>.

That’s it. When you click that link the script will set a cookie to remember your theme choice, then send you back to the page you clicked from and serve you the theme you’ve chosen instead of the default. To switch back all you’d need is another link:

<a href="styles.php?style=default">click here to switch back to default theme<a>

It’s that simple.

For a live example of this in action, swing by my site and click one of the style-switching buttons in the sidebar; for now the only choice I offer is between a fixed-width and a fluid layout, but I plan to add some more options shortly.

And there’s one other useful feature that you can use if you want to: you can upload a stylesheet called ‘global.css’ and un-comment one line in the script to have it included with each request; that way you can have some styles which are used no matter what particular theme someone has chosen. There are more detailed instructions for that in the script itself, if anyone’s interested.

Last edited by ubernostrum (2005-02-01 14:10:09)


You cooin’ with my bird?

Offline

#2 2005-02-01 14:31:07

paularms
Member
From: University of Minnesota
Registered: 2004-10-21
Posts: 155
Website

Re: A non-plugin style switcher

this seems to work just like the plugin except that this is much more work.

Offline

#3 2005-02-01 14:47:58

ubernostrum
Member
From: Lawrence, KS
Registered: 2004-05-05
Posts: 238
Website

Re: A non-plugin style switcher

This works indepenently of Textpattern; if you don’t want to use TXP’s built-in CSS system this gives you the option, and it won’t ever break with a future TXP upgrade. Plus you can use it on other PHP-powered sites as well. So I think it’s nice to have as an option.

It also offers the ability to specify a “global” stylesheet which always gets applied.

And if the comments in the plugin thread are any indication then this may be a bit easier to set up; the plugin produces invalid HTML and some people are apparently having problems getting it working.

Last edited by ubernostrum (2005-02-01 14:49:00)


You cooin’ with my bird?

Offline

#4 2005-04-05 19:17:55

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: A non-plugin style switcher

Thanks,

This works great. It wasn’t more work though. It actually seems a little easier and with more latitute to quickly change things around.

Jamie

Last edited by soulship (2005-04-05 19:19:23)

Offline

#5 2005-04-06 14:53:03

Daragh
Member
From: Toronto Canada
Registered: 2004-05-26
Posts: 60
Website

Re: A non-plugin style switcher

Yeah, as someone for whom the plugin stopped working upon upgrade, I’m happy to see this option out there. Thanks.

Offline

#6 2005-04-23 19:47:01

soulship
Member
From: Always Sunny Charleston
Registered: 2004-04-30
Posts: 669
Website

Re: A non-plugin style switcher

I have been playing with this and it was working on another server, but I am having some trouble on txd for some reason. I think I have it set-up the same, but I’m not sure. It seems as if it is not linking at all. I have both stylesheets in the site root and styles.php in the root as well, but I’m getting no style at all. I have tried switching everything to the txp directory, but that does’nt seem right and I still get nothing. Any other tips on this ubernostrum?

Thanks
Jamie

Offline

Board footer

Powered by FluxBB