HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Joomla PHP Template Logic

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpjoomlatemplatelogic

Problem

I'm in the process of building some templates for Joomla, and although it all works as desired, I can't help thinking that there has to be a more elegant way to do this in PHP than my current approach. For the record, I'm still reasonably new to PHP!

Using collapsible module positions, I need to assign various CSS classes to a div depending on how many modules are published on a particular row. The code I wrote is below;

if (($footer1 != "0") && ($footer2 == "0") && ($footer3 == "0")) {
    $footerWidth = 'full';
    }
    elseif (($footer1 == "0") && ($footer2 != "0") && ($footer3 == "0")) {
    $footerWidth = 'full';
    }
    elseif (($footer1 == "0") && ($footer2 == "0") && ($footer3 != "0")) {
    $footerWidth = 'full';
    }
    elseif (($footer1 != "0") && ($footer2 != "0") && ($footer3 == "0")) {
    $footerWidth = 'one_half';
    }
    elseif (($footer1 != "0") && ($footer2 == "0") && ($footer3 != "0")) {
    $footerWidth = 'one_half';
    }
    elseif (($footer1 == "0") && ($footer2 != "0") && ($footer3 != "0")) {
    $footerWidth = 'one_half';
    }
    else {
    $footerWidth = 'one_third';
    }


I then use the following code in the template itself to check for modules published;

 0) : ?>
                ">
                    
                
    
     0) : ?>
                ">
                    
                
    
     0) : ?>
                ">
                    
                
    


Can anyone suggest / recommend a better approach? It just seems like a lot of code for a simple function.

Solution

Something somewhat confusing is the comparison to of each $footer# to the string '0' then later comparing it to greater than integer 0. This is practically the definition of weakly typed languages. If you know what $footer# variables actually contain you might be able to simplify this, but here is a fully functioning replacement to the first code block.

One could simplify this a bit, but the core concepts are there. '0' casts to False, and True casts to 1. There are plenty of other ways to accomplish the same thing, especially if you have significantly more incoming $footer#'s, or if they are stored in something iteratable like an array.

//$footer1 = '10';
//$footer2 = '-5';
//$footer3 = '0';

$footer1Bool = (bool) $footer1;
$footer2Bool = (bool) $footer2;
$footer3Bool = (bool) $footer3;

$footer1Int = (int) $footer1Bool;
$footer2Int = (int) $footer2Bool;
$footer3Int = (int) $footer3Bool;

$footerCombined = $footer1Int + $footer2Int + $footer3Int;

if ( $footerCombined >= 3 ) {
    $footerWidth = 'one_third';
} else if ( $footerCombined == 2 ) {
    $footerWidth = 'one_half';
} else {
    $footerWidth = 'full';
}

Code Snippets

//$footer1 = '10';
//$footer2 = '-5';
//$footer3 = '0';

$footer1Bool = (bool) $footer1;
$footer2Bool = (bool) $footer2;
$footer3Bool = (bool) $footer3;

$footer1Int = (int) $footer1Bool;
$footer2Int = (int) $footer2Bool;
$footer3Int = (int) $footer3Bool;

$footerCombined = $footer1Int + $footer2Int + $footer3Int;

if ( $footerCombined >= 3 ) {
    $footerWidth = 'one_third';
} else if ( $footerCombined == 2 ) {
    $footerWidth = 'one_half';
} else {
    $footerWidth = 'full';
}

Context

StackExchange Code Review Q#12893, answer score: 2

Revisions (0)

No revisions yet.