Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

Adding Stylesheets in Joomla! 1.5

Joomla! 1.5 makes it really simple to add external CSS (or other) stylesheets into your component or even module templates. You can simply use the addStyleSheet method on the JDocument class.

For my GSoC project I used this in com_poll in the default.php template:


$doc =& JFactory::getDocument();
$doc->addStyleSheet('components/com_poll/assets/poll_bars.css');

This neatly inserts a link element inside of your page's head element. Although many popular browsers seem to support the link element for stylesheet includes even inside of the body element, this is not allowed according to the HTML 4.01 standard (and therefore also in XHTML 1.0). This appears to be the same in HTML 5 too (in the draft at the time of writing, unless I'm reading it wrong).

On my (outdated) checkout of the Joomla! trunk, the code it generates looks like this:

<head>
  [...]
  <link rel="stylesheet" href="components/com_poll/assets/poll_bars.css" type="text/css" />
  [...]
</head>

There are also a lot of other cool functions in the JDocument class such as the addScript method, etc. Also see the JDocumentHTML class with methods such as addHeadLink and addFavicon. Worth checking out, in my opinion!

0 Comments

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.