Skip to content

Display Modules Only When There is Content to Display

How to conditionally display modules within a Mambo template.

When designing our Mambo template we often wish to make provision for modules that may not always have content to display. Through the use of a Mambo function we are able to display these modules only when there is content to display on our site.

For example, if we intend to use banners or other advertising, but don't yet have any advertisers for our sites, we need to provide a spot for these to display once we have a banner to show, but we don't want an empty module position appearing before then. You will see example of this in the default Mambo templates.

To do this, we use a Mambo function named mosCountModules. Using this function in our templates tells Mambo, "if there is content in this module, then display it, otherwise just ignore it".

Example:

<?php if (mosCountModules('banner') >= 1)
{ ?>
<!-- start banner.  -->
 <div id="banner">
  <?php mosLoadModules('banner'); ?>
  </div>  <!-- end banner. -->
<?php } ?> 

This says, "if Mambo sees there is one or more banners to display, then load the banner module position" and if there is not >=1 banner then the module position will not be loaded at all.

In simpler terms..
Example:

<?php if (mosCountModules('banner') >= 1) { mosLoadModules('banner');  } ?> 

Note: this code can be used for any module position, including your own custom positions.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Topic: Mambo Tutorials
Tagged as: Mambo, modules, templates

Share on FriendFeed

{ 1 comment… read it below or jump to the comment form to share your opinion }

  1. 1 kard October 3rd, 2008 at 2:33 am

    thanks elpie...

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Any comments that look like spam will be treated as spam - this includes SEO titles and use of spurious keywords.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution.