Welcome to the new HowToJoomla!

We've completely revamped the design to make the site easier to read and easier to navigate. We hope you like the changes!

How to determine which page you are on from within a Joomla! 1.5 template

April 17, 2008 | by Cory | 23 Comments

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.

About the Author

Cory WebbIn the years since Joomla! was founded, Cory has built dozens of websites with Joomla! and helped thousands of people find answers to questions about Joomla! through HowToJoomla.net. Cory has also written a book about Joomla titled Beginning Joomla! Web Site Development published by Wrox in April, 2009. In February of 2008, Cory founded Cory Webb Media, LLC, where he provides consulting and web development services for companies of all sizes. You can follow Cory on Twitter @corywebb, or become a fan of Cory Webb Media on Facebook.

Read More

Trackback(0)

Comments (23)Add Comment

0
...
written by Jon, May 01, 2008
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?
62
...
written by Cory, May 01, 2008
Good question, Jon. You need to check for the value of "view". In this case, view=frontpage.
0
...
written by Lisa, May 19, 2008
But what if you are using SEF URL's? Is tehre still a way to achieve this?

Thank you by the way for an excellent blog - both entertaining and educational!

/Lisa
62
...
written by Cory, May 19, 2008
Hi Lisa... this should work with SEF URL's because you are using Joomla's "JRequest::getVar" command. I could go into a lengthy explanation of why this works, but basically Joomla automatically knows what the request parameters are even if you use SEF URL's.
0
...
written by links, May 25, 2008
this simple article is life safer !, i search all day in jm and vm forum but can't found this solution, one question : is my php right if need to hide from several component :

On top of page :



On body :


0
...
written by links, May 25, 2008
uh sorry, i think i need to quote my code :
On top of page :




On body :




0
...
written by links, May 25, 2008
On top of page :
[? $pageoption = JRequest::getVar( 'option', '' ); ?]
[? $pageview = JRequest::getVar( 'view', '' ); ?]

On body :
[? if ($pageoption == 'com_virtuemart') {echo "";}
elseif ($pageview == 'article') {echo "";}
else { ?]

[ } ?]

* change [ ] to < >
0
...
written by Fernando, May 27, 2008
Just a simple question, how do you add the colors in your articles for php coding for example, and how you format it like that?
0
...
written by Zee, July 03, 2008
As another option, couldn't you create another template and tell the menu item which template to use? Or do you find the method above easier?
62
...
written by Cory, July 03, 2008
Great question, Zee. My answer to that is, it depends on the situation. Sometimes you just need a small modification based on which page you are on. Making a separate template is a lot of work for a small modification when you can easily use this technique to make that modification within the same template.

Also, when you are dealing with multiple templates and you need to make a style change, you have to do it in more than one place. Keeping it all within one template limits the need to make changes in several places.

Thanks for the question, and thanks for visiting!
0
...
written by jairp heinrich, July 09, 2008
Hey, just found what I was looking for. Thanks a lot buddy! Keep up the good work.
0
...
written by Gijs, July 18, 2008
Hi,

I want a set of articles (not all articles) to have a different headers (h1, h2 etc. ).

Is there a clever to change the template within the article component?

Or should I better crate a copy of the component? (If so, do you know which?)

Thanks in advance,
Gijs
0
...
written by Sam, August 18, 2008
Thank you so much for this suggestion and sharing your knowledge. A real timesaver!

Cheers,
Sam
0
...
written by Doug, October 28, 2008
How do you do this technique in 1.0? I'm having a hard time finding documentation on the 1.0 template API. If you have a link for docs, that'd be awesome, too!

Thanks!
62
...
written by Cory, October 28, 2008
Hi Doug... you would use mosGetParam instead of JRequest::getVar. You can read more about mosGetParam here: http://help.joomla.org/content/view/516/155/
0
...
written by Mohsin, November 21, 2008
Wow, Cory this is what i was looking for!
i wanted the value for $view and $option but when SEF was turned ON
my whole template was GONE wild :-), as i was getting values from the URL... but thanks for your tips regrading JRequest!

Mohsin
0
...
written by Christian Skauge, December 15, 2008
Can't get this to work... probably because I have VirtueMart as frontpage as well. Any ideas there?

/Chris
62
...
written by Cory Webb, December 15, 2008
Hi Christian,

You can extend this technique further using JRequest::getVar and other variables passed in through the URL. For example, the "page" variable in Virtuemart might be very useful to you, so you could something like


$vmpage = JRequest::getVar('page','');


and then


if ($vmpage == 'somepagevalue') {
// perform some function
}
0
...
written by Dan Walker, January 08, 2009
I find it simpler to put everything into the php if statement like this:

Then you can change the getVar to whatever you want without creating a new variable every time.
0
Check for active menu
written by luke, March 16, 2009
In the template file I use the following if I need to alter the template depending on the current menu item name. Not sure how well it will scale if you have menu items with the same name, but works ok for small sites.

$menu = &JSite::getMenu();
$activeMenu = $menu->getActive();
$activeMenuName = $activeMenu->name;

if ($activeMenuName=="somepage"){
do something
}
62
...
written by Cory, March 16, 2009
Good tip, Luke. I prefer to use Itemid, because there are no duplicates. You get the Itemid like this:

$myitemid = JRequest::getVar('Itemid','');
0
Finally
written by Elkin, May 06, 2009
I used the code on here to figure out a solution. Basically, after some minor mods by me, the above helped me be able to hide things on every other page except the homepage. I love you guys! It took me over 5 hours to figure this out! I scourged all over the forums, and looked high and low, and no one knew anything. Thanks a bunch, you guys are geniuses!

I hope this code can help others that may need this...and saves them time.
I put this at the top of my index page:



I put this in the body of my code:


some stuff here.

some stuff here.



1595
getInt instead getVar for any id
written by Juan, February 23, 2010
Hi!!

Someone said:

$myitemid = JRequest::getVar('Itemid','');

You must use getInt('Itemid','') instead of getVar or you can have a security problem.

Write comment

You must be logged in to post a comment. Please register if you do not have an account yet.

busy