Code: Select all
$users = array(
array('name' => 'John Doe', 'age' => 32),
array('name' => 'Jane Doe', 'age' => 29),
array('name' => 'Jake Doe', 'age' => 42),
array('name' => 'Jaws Doe', 'age' => 22),
);
//
// Iterate array and output alternating colors as well as as boldifying text of anyone *over* 40
for($i=0; $i<count($users); $i++){
$clr = $i & 1 ? 'aeaeae' : 'eaeaea';
if($users[$i]['age'] > 40)
echo '<div style="background-color: #'.$clr.'"><b>'.$users[$i]['name'].'</b></div>';
else
echo '<div style="background-color: #'.$clr.'">'.$users[$i]['name'].'</div>';
}Anyways, the above is a rather trivial list generator...they alternate colors and boldify anyone who is *older& than 40 years of age...
Easy right? I am sure rendering logic *could* get a lot more complicated which is what I wanted, but for brevity and simplicity assume only the above...
Now...here's the challenge...discover a method/technique or whatever to cram that entire FOR loop into a single function call, all the while allowing a client developer full control over output, whilst *not* requiring any modifying of the rendering function...
I use a very simple native template engine and my code (templates) often get sprinkled with complicated looking for loops, etc...I'm investigating methods to condense the above logic into a flexible, efficient and easy to read macro of sorts (using functions as PHP doesn't support macros)...I've actually been working on a meta language of sorts which solves this very problem, but compilation overhead and complexity is making me consider alternatives...
So I ask...bring your ideas to the table...I'd like to see what an uninitiated community can do and how it differens from my own function.
p.s-I plan on using this function as a helper function inside my own template engine...incase your wondering
Cheers