Disclaimer: Ideally, this should/could be accomplished with Mark Norton’s mdn_if_custom plugin. But because that plugin is designed for use within articles and depends on variables that do not yet exist at the time non-article forms are parsed in Textpattern ($thisarticle & $pretext), this separate plugin was written and successfully sidesteps that issue.
Allows for conditional stylesheets on a per-article basis. Inspired by Khoi Vinh at Subtraction
There are three small, non-hack prerequisites for this to function:
- You must have a custom field available (defined in textpattern/lib/admin_config.php) in order to store the ‘true’ (or any non-empty) value, signaling that an article is using custom CSS.
- You must name the CSS file according to the article ID number. For instance, article 63 would use ‘63.css’.
- You must store all the CSS files in the same location.
- Number corresponding to the custom field you’re using. Any value whatsoever stored inside that custom field for an article will be seen as a boolean TRUE value. I just use a custom field named ‘CSS’ and type ‘1’ if the article uses custom CSS.
- OPTIONAL. A value such as ‘/css’ or ‘/assets/css’ (requires leading slash). If no path, default is relative to document root.
- OPTIONAL. By default, the stylesheet link tag will be output in XHTML format. By setting this value to ‘1’ you may opt for HTML.
- OPTIONAL. Allows for media types such as screen, projection, print, etc.
- OPTIONAL. Allows for assigning a title to the stylesheet.
This plugin is designed to be used within your main page template (or whichever form you use that contains the document <head>). Just simply put in the plugin tag after your normal style link tags and it will output article-specific link tags if the corresponding custom field has a value stored in it.
<txp:ajw_custom_css field="1" path="/css" />
<link rel="stylesheet" type="text/css" href="/css/63.css" />
<txp:ajw_custom_css field="1" nohtml="1" />
<link rel="stylesheet" type="text/css" href="/63.css" >
Changes in version 0.2
- Add support for ‘title’ attribute
- Add support for ‘media’ attribute