Joomla 2.5 Templates – $files = JHtml::_(stylesheet)

In this class, we are learning how to create Joomla 2.5 templates by first looking at the PHP code in the Beez2 index.php file. We have already looked at quite a bit of php code, and as we continue our review we come to the following code:

$files = JHtml::_('stylesheet', 'templates/'.$this->template.'/css/general.css', null, false, true);
if ($files):
        if (!is_array($files)):
                $files = array($files);
        endif;
        foreach($files as $file):
?>
            <link rel="stylesheet" href="/support/<?php echo $file;?>" type="text/css" />
<?php
         endforeach;
endif;

What is the value of $files?

The first line calls JHTML, and we can find out the value of $files by making the following adjustment:

$files = JHtml::_('stylesheet', 'templates/'.$this->template.'/css/general.css', null, false, true);
echo "<pre>"; print_r($files); echo "</pre>"; die();

When we look at the value of $files using the code above, this is what we see:

/templates/beez_20/css/general.css

At this point, we have an absolute URL to our general.css file. We only see one file listed, but the results could have included numerous other css files. What the template now wants to do is include each of these css files. If you see the line starting with foreach, it loops through all the files (such as general.css) and prints them in the following manner:

<link rel="stylesheet" href="/templates/beez_20/css/general.css" type="text/css" />

Isn’t JHtml suppose to include the CSS file? Why do we loop and print the code ourselves?

If you reviewed the JHtml link we posted above, JHtml was supposed to include the css file on its own, so why didn’t it? The entire section of code at the top of this article is in essence simply doing the same exact thing that JHtml could have done for us.

The last flag we passed to JHtml was true, which meant that we only wanted the files returned and not actually printed. If we set the value to false, as in:

$files = JHtml::_('stylesheet', 'templates/'.$this->template.'/css/general.css', null, false, false);

Then we would not have needed to include the rest of the code listed, which actually prints the HTML to include the css.

$files = JHtml::_('stylesheet', 'templates/'.$this->template.'/css/general.css', null, false, false);
if ($files):
        if (!is_array($files)):
                $files = array($files);
        endif;
        foreach($files as $file):
?>
            <link rel="stylesheet" href="/support/<?php echo $file;?>" type="text/css" />
<?php
         endforeach;
endif;

We hope this makes sense! If you have any questions about this, we’re happy to help if you ask us a question in our Community Support area.

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+