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.

Like this Article?

Login to comment.

Your Opinion Matters

... but we need to know what you're thinking!

I'm Brad Markle, your friendly Community Support technician, and I wrote the article you're looking at now. I like to think it's perfect, but I'm sure you have some suggestions. Please, let me know what they are!

Feedback
Your Email Address
Because we'd like to talk with you!

Latest Questions

If you need some help, submit your question to our Community!
We guarantee a response within 60 minutes (8am - 9pm EST, Monday - Friday)
Ask a Question!
Recent Questions
  1. register and order details
  2. 10+ hours offline and no solution. Why?
  3. How do you add new products using the Product Import extension?

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!