Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-10 00:24:49

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

sac_style_switcher

Based on Benoit Pepermans fine fla_style_switcher I made an extended switcher which allows a more refined control over alternate stylesheets.

The old style_switcher overrides all section based style settings in the preferences and applies one and the same stylesheet to all sections.
I wanted to keep those preferences as a base for my alternates. Means: By switching I want to keep different (alternate) stylesheets for different sections.
So now it is possible to switch to a set of alternate stylesheets by using a suffix.
The basic preference in the section’s settings will be taken into account.
This way you can even change a section’s basic style and its set of alternates without touching the rest.
And you don’t have to generate all alternates with suffix for there is a fallback option if an alternate isn’t present.

Old versions v0.1
Current version: sac_style_switcher 0.2

The basic help file I will post next .

I hope I didn’t miss something or overlooked a better solution.
And any help refining my code is welcome.

Last edited by saccade (2008-04-16 16:02:05)

Offline

#2 2008-01-10 00:43:33

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,475
Website GitHub

Re: sac_style_switcher

Very cool Michael, thanks.

I stopped using fla_style_switcher because of the TXP warnings it issues, and the fact it’s not supported much these days. Yours looks like a brilliant replacement with some nice new features. I’ve downloaded it and looked through the help. When I get a chance to play with it I’ll knock up some stylesheets and give it a spin.

Many thanks.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2008-01-10 00:48:47

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: sac_style_switcher

Dokumentation:

(Updated: namespace changed, instead of ?css= use now ?sac_css=)

This plugin is an extension of Benoit Pepermans fla_style_switcher 0.2.
To build style links, use his plugin fla_altstyle_link 0.1.
It is based on Chris Clarks Build a PHP Switcher (ALA 152).

sac_style_switcher allows to style-switch in a number of different ways:

A) Override all sections’ stylesheets with a single stylesheet

You can define a stylesheet to use on each of your sections.
This is achieved by defining a stylename beginning with an alphabetical character.
e.g. “blackandwhite” will apply the stylesheet “blackandwhite” to all sections, regardless which stylesheet originally is assigned to each one.

B) Switch the section’s stylesheet keeping different stylesheets for different sections

You can base switching on the section’s assigned stylesheet and matching variants.
This is achieved by defining a stylename suffix beginning with a hyphen.
Then a stylesheet based on the section’s stylesheet with the suffix attached will be used (of course it has to be created before).
e.g. “-contrast” will define the stylesheet “default-contrast” in the default section and the stylesheet “calendar-contrast” in a section using the calendar-stylesheet.

Handling the switch

1) You can define the stylesheet by attaching ?sac_css=stylename to the url.
e.g. http://www.mydomain.com/?sac_css=shiny
This will set a cookie and the defined style will be used until you define another one or the cookie expires (after 1 month of not viewing the site).
To clear the cookie, attach an empty ?sac_css= to the url.
e.g. http://www.mydomain.com/?sac_css=
After that the default sections’ stylesheets will be used again.
(Notice: If you have defined a default style within the plugin’s tag – see below – it will always get active unless you override it explicitly by ?sac_css=.)

2) If you define a default-style within the plugin’s tag, the defined stylesheet will be used unless you define a new one (saved by the cookie).
If you want to get back to the original section’s stylesheet, attach an empty ?sac_css= to the url.
This will clear the cookie and override the plugin default and show the page with the original section’s stylesheet.
(Notice: If you subsequently call pages without ?sac_css= you will always get the stylesheet defined by the plugin’s tag default attribute.)

Usage:

Replace the standard call to the css file in the “head” section of your page templates with : <txp:sac_style_switcher />.
You can specify a default stylesheet : <txp:sac_style_switcher default="otherdefault"/>
and specify the media this stylesheet is dedicated to : <txp:sac_style_switcher media="print" />
For stylesheet linking, please see the fla_altstyle_link plugin

There is a Fallback for stylename suffices
Due to its function the plugin would normally apply a suffix to each stylesheet used by any section. But possibly there are sections/stylesheets that don’t need an alternative (or you haven’t styled yet).
To avoid missing stylesheets and the resulting plain HTML you’d have to build dummy stylesheets (identical copies) with the suffix applied. But obviously that’s not very useful.
So by default there is an automatic fallback: If a stylesheet with the matching suffix is missing, the styleswitcher will use the base stylesheet without a suffix.
If you don’t want that fallback, e.g. for testing, simply set the attribute to "no".
e.g. <txp:sac_style_switcher fallback="no" />
Without fallback missing stylesheet-variants will be shown as plain html.
The fallback normally doesn’t apply to full defined stylenames (A). For this purpose there is an attribute fallback=“full”, which includes suffix fallback.
e.g. <txp:sac_style_switcher fallback="full" />

Example

Here you can see, what happens with each formula. Imagine, you have the sections “growth, silence, life, crisis, article, about”.
And you have prepared stylesheets with the possibilty to switch between “tastes” for most of them, except “fire, type and color”.

Available stylesheets:

wood desert water fire type color fullview
wood-europe desert-europe water-europe - - - -
wood-america desert-america water-america - type-america - -
wood-africa desert-africa water-africa - - color-africa -
wood-asia desert-asia water-asia - - - -

And these stylesheets get linked to by different tags (or a cookie):

section section css-<br />preference ?sac_css=fullview ?sac_css=-europe<br />fallback=“no” ?sac_css=-africa<br />fallback=“yes” ?sac_css=-america<br />fallback=“yes”
growth wood fullview wood-europe wood-africa wood-america
silence desert fullview desert-europe desert-africa desert-america
life water fullview water-europe water-africa water-america
crisis fire fullview fire-europe* fire fire
article type fullview type-europe* type type-america
about color fullview color-europe* color-africa color

*= not available, showing plain html

Last edited by saccade (2008-01-10 08:46:42)

Offline

#4 2008-01-10 00:56:42

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: sac_style_switcher

@Bloke:
Welcome!
I hope everything is fine and my humble english explanation makes myself clear.

But now it got late. See you tomorrow.

Offline

#5 2008-01-10 03:54:57

iblastoff
Plugin Author
From: Toronto
Registered: 2006-06-11
Posts: 1,197
Website

Re: sac_style_switcher

hmm so you need fla_altstyle_link plugin as well? except the link given on the textpattern.org page gives a 404

Offline

#6 2008-01-10 04:46:42

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: sac_style_switcher

hmm so you need fla_altstyle_link plugin as well?

No. It seems to be working with the url http://www.mydomain.com/?css=my-css-file

So need for that plugin, if I understand correctly – you can just do the changer with fla_altstyle_link.

Cheers!

Offline

#7 2008-01-10 05:23:00

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: sac_style_switcher

Hi, good morning!
Gocom is right, it’s simply working with the url and you can build your switching links, changer menus and so on by yourself. fla_altstyle_link just lets you build links with a surrounding tag and maybe in this way easier by an automated list if needed.

I think about including the code in sac_style_switcher, but I have to review it first.

And I will correct my explanation about fla_altstyle_link so it is an option to use it.

Offline

#8 2008-01-10 07:56:16

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

Re: sac_style_switcher

saccade wrote:

http://www.mydomain.com/?css=shiny

Please refrain from using generic $_GET variable names (as $_GET['css'] in this case). According to Murphy’s law, another plugin author or even TXP core will use the same name for a very different purpose and thus clash as early as tomorrow.

As a plugin author, you are well advised to use a prefix on each and every item you introduce into the Textpattern namespace.

Offline

#9 2008-01-10 08:21:05

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: sac_style_switcher

@wet
Thank you for the advice.
I took that over from fla_style_switcher and to be matching to fla_altstyle_link.
Sorry I didn’t have the prefix in mind for URL parameters.
So it would be better to use ?sac_css= for the url parameter and sac_stylesheet for the cookie.

@all
I will change that for version 0.2 (coming in a few hours minutes).

Last edited by saccade (2008-01-10 08:48:21)

Offline

#10 2008-01-10 08:40:04

saccade
Plugin Author
From: Neubeuern, Germany
Registered: 2004-11-05
Posts: 521

Re: sac_style_switcher

I updated the plugin, now reflecting the plugin namespace.

Download it here

This was a fast fix and now I have to leave for some hours, so I hope there isn’t any problem left.
Now it isn’t compatible with fla_altstyle_link anymore, unless you modify its url parameter there.
But I’m working on a similar tag for generating links.

Last edited by saccade (2008-04-16 16:03:02)

Offline

Board footer

Powered by FluxBB