DooPHP IRC channel


View Template: For Loops

Discussion about new desired features.

View Template: For Loops

Postby RichardM » Fri Dec 11, 2009 1:40 am

Okay so there is support for looping over an array but what if you want to loop over say the values 0-10 or all even numbers from 0-100? or possibly even something like for(i=0; i<{{obj.@total}}; i++) ???

I'm thinking about adding this into DooView but I can not settle on a syntax to use and also a way to allow for complex (every even number or other math functions) and also to allow the use of currently running loops so you might have

Code: Select all
<!-- loop users -->
<!-- for:ForLoopName | 0 | < | users' value.@something | ++ -->
{{ForLoopName}}
<!-- if (ForLoopName % 5 == 0) -->
<br />
<!-- endif -->
<!-- endfor -->
<!-- endloop -->

This would become something like
Code: Select all
<?php for ($ForLoopName = 0; $ForLoopName < $v2->something; $ForLoopName++) { ?>
...
<?php } ?>


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

Re: View Template: For Loops

Postby leng » Fri Dec 11, 2009 4:55 am

how about:
Code: Select all
<!-- loop 0-100 -->

<span>{{l' v}}</span> (this would be the number)

<!-- endloop -->


to loop to variable
Code: Select all
<!-- loop 0-{{total}} -->

<span>{{l' v}}</span> (this would be the number)

<!-- endloop -->


from and to (both variables)
Code: Select all
<!-- loop {{start}}-{{total}} -->

<span>{{l' v}}</span> (this would be the number)

<!-- endloop -->
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: View Template: For Loops

Postby RichardM » Fri Dec 11, 2009 8:43 am

Leng,

I rather like the syntax. My only thought is I want the for loop to be independent of loops used to iterate over array date passed to the templates.

Code: Select all
<!-- for {{start}}-{{total}} -->
<span>{{f' v}}</span> (this would be the number)
<!-- endloop -->


Also slightly off topic but still semi related. You need to be careful of using the l v' v' syntax in examples of the looping as nested loopsonly seem to work properly using the value' and not v' having tested it last night and the only solution i could see was to end up testing for both v' v' and value' value'. Additionally if you choose one method you should stick with it and avoid {{foo' value' v' value' v.@bar}}


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

Re: View Template: For Loops

Postby RichardM » Tue Dec 15, 2009 12:44 pm

I'm going to give this a shot with the following syntax
Code: Select all
<!-- for KEY_VAR from KEY_FROM to KEY_TO -->
    {{KEY_VAR}}<br />
<!-- endfor -->


which would be used something like this something like
Code: Select all
<!-- for index from 0 to {{l' v' v.rank}} -->
    <img src="rank_img_part_{{index}}.gif" />
<!-- endfor -->


And possibly an optional part can be added now or later to allow for incrementby???

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

Re: View Template: For Loops

Postby RichardM » Tue Dec 15, 2009 1:14 pm

Okay. Got this working for a simple

for (i = from; i < to; i++)

But how do you suggest picking the operator because I can see people wanting to do
for (i = from; i > to; i++)


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

Re: View Template: For Loops

Postby leng » Tue Dec 15, 2009 1:32 pm

Best with the operator, familiar to both PHP/JS people and I can do reversed loop with i--
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: View Template: For Loops

Postby RichardM » Tue Dec 15, 2009 2:28 pm

Right so this is what I have come up with:

Code: Select all
// A simple loop from 0 to 10
<!-- for i from 0 to 10 -->
   {{i}},
<!-- endfor -->
// Renders 0, 1, 2..., 9, 10

// A simple loop from 0 to 10 with steps of 2
<!-- for i from 0 to 10 step 2 -->
   {{i}},
<!-- endfor -->
//Renders 0, 2, 4..., 8, 10

// Counting backwards
<!-- for i from 10 to 0 step 3 -->
   {{i}},
<!-- endfor -->
// Renders 10, 7, 4, 1

// With some data stored in template variables
//$data['someNumberInData'] = 10
<!-- for i from 0 to {{someNumberInData}} -->
   {{i}},
<!-- endfor -->
// Renders 0, 1, 2..., 9, 10

// In loop
<!-- loop users -->
<!-- for i from 0 to {{users' v.@rank}} -->
   {{i}},
<!-- endfor -->
// Assuming 1 user with a rank of 5
// Renders 0, 1, 2, 3, 4, 5


The code it self is compiled down to something along the lines of
Code: Select all
<?php foreach(range(10, 0, 3) as $data['i']): ?>
   <?php echo $data['i']; ?>
<?php endforeach; ?>


So as you can see the variable is stored into the data array. This is for 3 simple reasons:
  • You don't have to start the for statement as <!-- for' v' v' to get to a sensible level to use with running for loops
  • You can change the value using logic within the for section
    Code: Select all
    <!-- if ({{i}} == 3) -->
         <!-- set i as 5 -->
    <!-- endif -->
  • You can nest one for statement inside another
    Code: Select all
    <!-- for i from 10 to 0 step 3 -->
       <!-- for j from 0 to 10 step 2 -->
          {{i}} : {{j}}
       <!-- endfor -->
    <!-- endfor -->

I'll be submitting this shortly


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

Re: View Template: For Loops

Postby RichardM » Mon Dec 28, 2009 2:05 am

I've added some additional functionality to the for command and added a new variable output tag as well. These are used to iterate over 'named' arrays so you can iterate over one array inside another where the second array is not the child to its parent. This works like this:

Code: Select all
<ul>
<!-- for users as user -->
<li>{{$user.@id}} :: {{$user.@name}}</li>
<!-- endfor -->
</ul>


Or if you want to iterate with a key you can do
Code: Select all
<ul>
<!-- for users as user_key=>user -->
<li>{{$user_key}} :: {{$user.@name}}</li>
<!-- endfor -->
</ul>


You can also use the {{$foo}} inside of other tags like
Code: Select all
<!-- if ({{$foo}} == 1) -->
I'm first
<!-- endif -->



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

Re: View Template: For Loops

Postby leng » Mon Dec 28, 2009 12:41 pm

that's nice!
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: View Template: For Loops

Postby RichardM » Mon Dec 28, 2009 1:05 pm

I may extend this shortly so that you can do
Code: Select all
<!-- for $key as subkey=>$subvar -->

The $ in front of the key would let you go into a subloop so you may have
Code: Select all
<!-- for users as user -->
<!-- for $user.@roles -->


or something similar to the above anyways...will provide more info when i come to a decision on this. For now though the key part has no $ at the start and is an identifer to some array in $data. You need to use the <!-- loops to get at sub arrays of a parent.


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

Next

Return to Features Request

Who is online

Users browsing this forum: No registered users and 2 guests