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="/support//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.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve our Support Center:
Email Address
Optional, but our team may contact you for more information.
Like this Article?

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!