News Feed E-mail

Get How-To's right in your inbox. Subscribe to the HowToJoomla! news feed e-mail. Just enter your email address here:

Delivered by FeedBurner

Login






Lost Password?
No account yet? Register

Affiliates

Azrul's JomComment
JoomlaShack
iJoomla
The Joomla! Store
Member of W3C Sites dot com
How to determine which page you are on from within a Joomla! 1.5 template PDF Print E-mail
Written by Cory   
Thursday, 17 April 2008

One thing I often need to do while developing a template is change certain parts of the template depending on which page I am on. For example, let's say I am developing a template for a site that runs Virtuemart, and I want to have 3 columns on every non-Virtuemart page, and 2 columns on every Virteumart page. A solution that I often like to use is to make the column "collapsable" when the value of "option" is "com_virtuemart". When "option" is equal to "com_virtuemart", that simply means that the Virtuemart component is loaded.

Where is the value of "option", and how do I get it?

If you are not using SEF URL's, you can easily see where the value of "option" comes from. For a Virtuemart page, the URL will look something like this:

"index.php?option=com_virtuemart&Itemid=3"

You'll notice that in the URL, the value of "option" is "com_virtuemart". This is the most common way of passing parameters from one page to another in Joomla! It is called a GET request, which simply means that parameters are passed at the end of the URL. (Another type of request is POST, which passes parameters without appending them to the URL. The technique I will show you in this article will get the parameter whether is is passed through a GET or a POST request.)

That's great, but how do I get that value in my template?

I'm glad you asked, because this is the most important part of determining what page you are on from within the template. At the top of your template "index.php" file, add the following code:

 
<?php
 
$pageoption = JRequest::getVar( 'option', '' );
 
?>

"JRequest" is a Joomla! object that is used for getting variables passed through an HTML request (GET, POST, or SESSION). The "getVar" method returns the desired value as a string. The first parameter in the "getVar" is the name of the value we want to retrieve. In this case, we want to get the value of "option", so we set the first parameter to 'option'. The second parameter is a default value, just in case the parameter we are looking for has no value. I usually set that to an empty string, but you could put just about anything you want there. For example, if there is no "option" (not likely in Joomla), then the value of $pageoption in the code above will just be an empty string ('').

Yes, but what do we do with this information?

Now that we know the value of "option", we have a lot of flexibility to modify the layout of our template depending on which component is loaded. Here is how you might do it:

 
<?php if ($pageoption == 'com_virtuemart') { ?>
<!-- Put some HTML here that you want to show while Virtuemart is loaded -->
<?php } else { ?>
<!-- Put some HTML here that you want to show while Virtuemart is not loaded -->
<?php } ?>

Hiding a module position

Here is how you use this concept to hide a module position when Virtuemart is loaded:

 
<?php if ( $pageoption != 'com_virtuemart' && $this->countModules('moduleposition') ) { ?>
<div class="collapsiblecolumn">
 <jdoc:include type="modules" name="moduleposition" />
</div>
<?php } ?>

In plain English, this code basically says if Virtuemart is not loaded and there are modules published for "moduleposition", then show the modules in "moduleposition". The name "moduleposition" can be any name you choose.

Questions/Comments

As always, questions and comments are welcome.

Trackback(0)
Comments (2)add comment
Great tip for the Joomla masses. I use this technique a lot as well.

One question I have though, since I'm new to Joomla 1.5 I'm finding it hard to figure out if I'm on the frontpage. In 1.0.x $option would equal com_frontpage, but in Joomla 1.5 com_content is used for the frontpage as well as regular content.
Do you know of a simple way to distinguish the frontpage from within the template?
1

report abuse
vote down
vote up
May 01, 2008
Votes: +0
Good question, Jon. You need to check for the value of "view". In this case, view=frontpage.
2

report abuse
vote down
vote up
May 01, 2008
Votes: +0

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 
Tag it:
Delicious
Digg
Technorati
Next >
iJoomla

About HowToJoomla

HowToJoomla was started in September 2006 by me, Cory Webb, a long-time Mambo/Joomla user with a desire to give back to the community that has given so much to him.

HowToJoomla is not the best, most comprehensive Joomla tips site out there. That's not the goal. The goal is to share some of the knowledge and experience that I have gained through years of using the world's best content managment system. I hope that you will find this site useful and visit often. As I come across new tips and ideas, I will post them here for all to see.

I hope you enjoy using HowToJoomla and find it very useful.

Please take some time to read the Disclaimer.

Thanks for visiting!

About Me

I am a 28-year-old husband and father of a beautiful baby girl named Lucy. I have a degree in Electrical Engineering from the University of Texas at Austin, an MBA from Baylor University, and over 4 years experience working with the Mambo/Joomla CMS.

On February 1, 2008, I started a web design, development, and consulting company specializing in designing and developing custom Joomla-powered websites. The new company is aptly named Cory Webb Media, LLC. The double meaning is purely unintentional, but with a last name like "Webb" it is difficult to escape the obvious puns. If you are looking for professional assistance with your website, please feel free to contact me to discuss your project.

I also run HowToJoomla, CoryWebb.com (my personal website/blog), and JoomlaForm.com (a site dedicated to promoting good design in the Joomla! community). In my spare time (if there is such a thing), I volunteer with the youth ministry at my church.

Copyright © 2006 - Cory Webb Media, LLC - All rights reserved.
Powered by Joomla!