Counting modules in multiple module positions
From Joomla! Documentation
Basic usage of the countModules method is explained here.
The countModules method can be used to determine the number of Modules in one given Module position.
The argument to the countModules function is normally just the name of a single Module position. The function will return the number of Modules currently enabled for that Module position.
Since Joomla! 3.0 all official templates use plain PHP code to calculate the number of multiple modules positions. This code calculates the content width, and is found in the index.php file of Protostar:
// Adjusting content width
if ($this->countModules('position-7') && $this->countModules('position-8'))
{
$span = "span6";
}
elseif ($this->countModules('position-7') && !$this->countModules('position-8'))
{
$span = "span9";
}
elseif (!$this->countModules('position-7') && $this->countModules('position-8'))
{
$span = "span9";
}
else
{
$span = "span12";
}
This code is used in the main area of the template and automatically arranges the width of it. So if the user enabled some modules in the left side, the widths will become span9 and span3 respectively. (span12 is full width.) If the user enables both left and right, the widths will become span3 span6 span3. And if no modules are enabled, either left or right, the width will become span12. (A full row.)