DooPHP IRC channel


dooviewbasic cycle

Discussion about new desired features.

dooviewbasic cycle

Postby ximian » Tue Feb 16, 2010 7:20 pm

Hi,

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
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: dooviewbasic cycle

Postby RichardM » Tue Feb 16, 2010 8:08 pm

Hey,

Why not just use the new filters? ;)

In template tags define a function as follows:
Code: Select all
public static function filter_cycle(&$num, &$options) {
   return $options[$num % count($options)];
}


Code: Select all
{% set colours as ['#f00', '#0f0', '#00f'] %}
{% for x from 1 to 10 %}
<span style="color:{{x|cycle(colours)}}">Value {{x}}</span><br />
{% endfor %}
<hr />
{% for y from 1 to 10 with meta %}
<span style="color:{{doo.for.y.index|cycle(colours)}}">Value {{y}}</span><br />
{% endfor %}


Note the second example uses the meta data to give us the index value for each looped element as we can not garantee that y would increment by one each time. For example try:
Code: Select all
{% for z from 1 to 10 step 3 %}
<span style="color:{{z|cycle(colours)}}">Value {{z}}</span><br />
{% endfor %}


And they will all be green


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: dooviewbasic cycle

Postby ximian » Tue Feb 16, 2010 9:52 pm

RichardM wrote:Why not just use the new filters? ;)


Cycle should be independent of for loop. On examples, you are looping colors array, not the data array.. and using a filter we need to change a dummy or reference value.. and be assigning that with for meta becomes difficult to read.

On my previous example, we only need a single line and that's it.. no additional loops.. just a simple line

Francisco A
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: dooviewbasic cycle

Postby ximian » Wed Feb 17, 2010 12:02 am

Richard,

Do you see any problem adding the cycle tag?

Francisco A
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: dooviewbasic cycle

Postby RichardM » Wed Feb 17, 2010 12:15 am

I never added another loop to the example you gave. Also your examples do have loops in them...otherwise if your hard coding say an entire table just add the css styles in manually when you write it. Taking your example and assuming the filter I suggested is defined you can simply write:
Code: Select all
{% set colours as ['#eeeeee','#d0d0d0'] %}
{% for table as row with meta %}
<tr bgcolor="{{doo.for.row.index|cycle(colours)}}">
   <td>{{row.id}}</td>
</tr>
{% endfor %}


OR

Code: Select all
{% for table as row with meta %}
<tr bgcolor="{{doo.for.row.index|cycle(['#eeeeee','#d0d0d0'])}}">
   <td>{{row.id}}</td>
</tr>
{% endfor %}


I'll see about writing a block for it but I may well move the assignment of the array to the top of the compiled code so it does not have to keep checking if its been assigned as the cycle's code is ONLY of any use within a for loop. For now though the filter option I have suggested can/will do the same thing until I get such a tag added.


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Re: dooviewbasic cycle

Postby ximian » Wed Feb 17, 2010 9:11 am

Well, i prefer to wait then. This is not critical at all and can wait. I really prefer a cleaner syntax.

Francisco A
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: dooviewbasic cycle

Postby RichardM » Sun Feb 28, 2010 2:48 am

Hi,

This has now been added. Will upload the revised code shortly...just working to add the 'insert' tag while I'm at it.

Usage information can be found here: viewtopic.php?f=7&t=1015&p=4388#p4388


Richard
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK


Return to Features Request

Who is online

Users browsing this forum: No registered users and 0 guests

cron