countModules

The countModules function is used in Joomla! to count module positions. Thus can be queried whether a particular position is currently active on the current page.

Since there are dynamic module positions is used in JYAML, is the in Joomla! integrated countModules function not suitable for every case. There are also in the Layout Options a setting "Count Empty Modules" what additional content should be checked also. For this reason, the countModules function has been expanded.

Basically, for what the function does, is in the Joomla! Documentation described: http://docs.joomla.org/JDocumentHTML/countModules

Basically nothing has changed. Only if you want to check the special dynamic template positions the following syntax is.

Counting of a normal module position

This is regardless of whether the position is in a dynamic position.

See Joomla! documentation: http://docs.joomla.org/JDocumentHTML/countModules

Counting of a dynamic position including all sub-positions

For example we have the dynamic position "col1_content". This contains the module positions left1, left2 and left3.

Example: $this->countModules('col1_content') or $this->countModules('col1_content:*')

As you can see, this works just like a normal module position count. Only with the difference that all sub-positions are also checked.

Counting of a specific sub-position within a dynamic position

For example we have the dynamic position "col1_content". This contains the module positions left1, left2 and left3.
Now we want to count only left2 within col1_content.

Example: $this->countModules('col1_content:left2')

As you can see the dynamic position and the module position separated with a colon (:).

Other examples

Joomla! furthermore has some special positions, for example of type "component" or "message". Normally these can be checked with the getBuffer function. Especially these two types can also be tested directly in JYAML with countModules. This requires a prefixed hash sign (#).

Example: $this->countModules('col3_content:#component')

Following are some examples with comparison operators.

  • $this->countModules('col1_content OR col2_content OR col3_content')
  • $this->countModules('col1_content:left AND col2_content:right XOR col3_content:col3_top')
  • $this->countModules('main_navigation:* XOR top_navigation:*')

For real world examples easily take a look in the HTML-Templates in /html/index/.

Tip: Call within Joomla! Template Output Override files

If you are not in the main HTML template files the variable $this is not a object of JYAML or not a object of JDocumentHTML. Here you must first call the instance to get the right object reference.

With JYAML:

$document = JYAML::getDocument();
$document->countModules('...');

Without JYAML (will work with any Joomla! Template)

$document = JFactory::getDocument();
$document->countModules('...');