Sometimes we need to cycle some values inside a for loop. Imagine a simple example where we output a table:
- Code: Select all
{% for table as row .. %}
<tr bgcolor="#eeeeee">
<td>{{row.id}}</td>
</tr>
{% endfor %}
Most of time, designers want to alternate colors, class names, and so on inside the loop.. and this is a standard way of doing things.. nothing special here.
Some template engines have this task much easier to handle.
What do you think of a new tag for this eg: cycle?
- Code: Select all
{% cycle ['#eeeeee','#d0d0d0'] %}
- Code: Select all
{% for table as row .. %}
<tr bgcolor="{% cycle ['#eeeeee','#d0d0d0'] %}">
<td>{{row.id}}</td>
</tr>
{% endfor %}
so this become:
- Code: Select all
<tr bgcolor="#eeeeee">
<td>1</td>
</tr>
<tr bgcolor="#d0d0d0">
<td>2</td>
</tr>
<tr bgcolor="#eeeeee">
<td>3</td>
</tr>
Well, how can this be implemented? What do you think about a global doo for variable, so that tag
- Code: Select all
{% cycle ['#eeeeee','#d0d0d0'] %}
can be rendered as something like
- Code: Select all
<?php if( ! isset($data['doo']['cycle']['cid'])){
$data['doo']['cycle']['cid'] = array('#eeeeee','#d0d0d0');
end($data['doo']['cycle']['cid']);
};
if( next( $data['doo']['cycle']['cid'] ) == false ) print reset($data['doo']['cycle']['cid']);
else print current($data['doo']['cycle']['cid']) );
?>
where cid is a generated id on rendering process.
What do you think?
Francisco A
