2

I'm using grid layout and I need to calculate column width depending on amount of active modules in a position. I'm assigning the css class within my module chrome.

What I was doing till now was something like this:

$position1ColumnWidth = functionThatCalculatesWidth('position1');
...
<jdoc:include type="modules" name="menu" style="customChrome" colWidth="<?php echo $position1ColumnWidth?>"/>

And than in modules.php I'd do something like this:

function modChrome_customChrome ($module, &$params, &$attribs) {
    echo "<div class=\"" . $attribs["colWidth"] . ">";
    echo $module->content;
    echo "</div>";
}

This way seems dirty to me because I have to calculate column width in my index php and it adds unnecessary code if I'd like to add a new position.

Is there a way to access countModules function for position that is currently rendered from within module chrome to separate logic from template as much as possible?

1 Answer 1

1

A chrome function like this could work:

function modChrome_mymod($module, &$params, &$attribs)
{   
    jimport( 'joomla.application.module.helper' );
    $class = "";
    if(count(JModuleHelper::getModules('position'))) {
        $total_modules = count(JModuleHelper::getModules('position'));
        $width = round(100 / $total_modules);
        $class = " width-".$width;
    }
    if (!empty ($module->content)) : ?>
        <div class="gridmod<?php echo $class; ?> floatleft moduletable<?php echo htmlspecialchars($params->get('moduleclass_sfx')); ?>">
    // Rest of your chrome function goes here ...
}

It counts the total modules of the given position, then divide 100/ number of modules and assign this to a class name.

You can then create styles for these expected class names like:

.width-100 {width:100%;}  
.width-50 {width:50%;}  
.width-33 {width:33%;}  
.width-25 {width:25%;}  

and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.