Online Joomla Training

Joomla! How-To's: Templates

Making module positions viewable only by unregistered visitors

September 12, 2006 | by Cory

Here is a trick for making your module positions viewable only by unregistered visitors.

There are some powerful programming objects in the Joomla! API that are available to template designers. One such object is the user object, which is accessible with the php variable $my.

If you want to determine whether or not a visitor is a registered user, you simply need to add the following code:

 
<?php
 
  if ( $my->id ) {
    // Code to generate if visitor is a registered user
  } else {
    // Code to generate if visitor is not a registered user
  }
 
?>
 

This code basically just says "If this visitor has a user id, then generate the first set of code. Otherwise, generate the second set of code."

We can now take this idea one step further. Let's use this trick we've just learned to tell our template not to display a module position to registered users.

 
<?php
  if ( !$my->id ) {
    mosLoadModules( 'guests_only_position', -2 );
  }
?>
 

Notice the "!" before $my in this example. You interepret this by adding the word not. So this code basically says, "If this visitor does not have a user id, then load the modules that are assigned to the module position 'guests_only_position'." (Please note that "guests_only_position" is not a standard module position in Joomla!. I only used it for the sake of this example.)

This trick is useful if you have items that you do not want to show your registered users. For example, you might have a banner inviting visitors to join your site. Your registered users do not need to see this banner, so you can use this trick to hide it from them.

There are a number of possible uses for this trick in your Joomla! template. I will leave it up to you to come up with more creative ways to use it.

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. Cory is also the founder of Themeables, a template club for Joomla!, and developer of /motif, the framework that powers Themeables.

Read More

blog comments powered by Disqus