Re: [PHPTAL] while loop in PHPTAL

2010-01-04 Thread Iván Montes
Yeah, I thought about using LimitIterator [1] to avoid the creation of 
temporary arrays but I wasn't sure about the performance of it (does it 
always seek?) or if it works with Traversables.


A custom LimitIterator, without seeking, might of course work, or 
perhaps it's enough to just modify the code and use a SplFixedArray 
object to ensure constant memory usage, without relying on PHP's garbage 
collector to do the job.


[1] http://php.net/limititerator

/imv

On 1/4/10 3:19 PM, Kornel Lesiński wrote:

On 31-12-2009 at 15:23:20 Iván Montes drsl...@pollinimini.net wrote:

I've put together a very simple chunk iterator [1] which works with 
arrays, traversables and iterators. There is also an example tales 
modifier.


Perhaps it could even be added to the standard PHPTAL code base?

[1] http://gist.github.com/266759


Thank you.

That's a good start, but doesn't avoid creation of arrays yet. What I 
actually had in mind was pair of iterators (one for rows like you've 
created, another for each row) that doesn't create PHP array at any 
point, just passes calls to the innermost iterator. That would ensure 
constant memory usage (rather than 1/number_of_columns) on arbitrarily 
large iterations, which I think is the major reason to use iterators 
instead of (presumably faster) array_chunk(iterator_to_array()).




___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] while loop in PHPTAL

2009-12-31 Thread Kornel Lesiński

On 31-12-2009 at 04:44:34 romtek rom...@gmail.com wrote:


Thank you very much  for this sample! It's much more elegant, easier to
understand and maintain than what I've come up with. Things like this had
better be published in a special section of documentation. And I hope  
that
array_chunk() doesn't copy data needlessly. This method does introduce  
more

overhead.

Kornel, what would you do if data were a collection but not an array
(besides using something like what was suggested by Rasmus Schultz at
http://php.net/manual/en/function.array-chunk.php)?


If it's small enough to fit in memory, then probably conversion to array  
with iterator_to_array() will be fastest.


Otherwise you could write something like IteratorIterator  
http://www.php.net/IteratorIterator that ignores reset() and pretends it  
ends every n elements.


--
regards, Kornel

___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] while loop in PHPTAL

2009-12-31 Thread Iván Montes

Hi,

I've put together a very simple chunk iterator [1] which works with 
arrays, traversables and iterators. There is also an example tales 
modifier.


Perhaps it could even be added to the standard PHPTAL code base?

[1] http://gist.github.com/266759

regards,
/imv

On 12/31/09 11:02 AM, Kornel Lesiński wrote:

On 31-12-2009 at 04:44:34 romtek rom...@gmail.com wrote:


Thank you very much  for this sample! It's much more elegant, easier to
understand and maintain than what I've come up with. Things like this 
had
better be published in a special section of documentation. And I hope 
that
array_chunk() doesn't copy data needlessly. This method does 
introduce more

overhead.

Kornel, what would you do if data were a collection but not an array
(besides using something like what was suggested by Rasmus Schultz at
http://php.net/manual/en/function.array-chunk.php)?


If it's small enough to fit in memory, then probably conversion to 
array with iterator_to_array() will be fastest.


Otherwise you could write something like IteratorIterator 
http://www.php.net/IteratorIterator that ignores reset() and pretends 
it ends every n elements.




___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] while loop in PHPTAL

2009-12-30 Thread Kornel Lesiński

On 29-12-2009 at 22:06:13 romtek rom...@gmail.com wrote:


I was looking for a more elegant way to generate grids using PHPTAL that
what I've been using and stumbled upon this post:
http://lists.motion-twin.com/pipermail/phptal/2007-February/000730.html.  
In

it, Aaron McClimont has proposed adding a while loop (and also
dowhile)construct to PHPTAL. I think it's very valuable as it allows for
much more elegant code in certain situations. Has this been added to the
library? If not, could it be?


Are there any other uses for while?

Variable number of columns can be done more elegantly than by _copying   
pasting_ code, and it can even vary at run time:


tr tal:repeat=row php:array_chunk(data, number_of_columns)
  td tal:repeat=col row tal:content=col /
/tr


--
regards, Kornel

___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] while loop in PHPTAL

2009-12-30 Thread romtek
2009/12/30 Kornel Lesiński kor...@aardvarkmedia.co.uk

 On 29-12-2009 at 22:06:13 romtek rom...@gmail.com wrote:

  I was looking for a more elegant way to generate grids using PHPTAL that
 what I've been using and stumbled upon this post:
 http://lists.motion-twin.com/pipermail/phptal/2007-February/000730.html.
 In
 it, Aaron McClimont has proposed adding a while loop (and also
 dowhile)construct to PHPTAL. I think it's very valuable as it allows for
 much more elegant code in certain situations. Has this been added to the
 library? If not, could it be?


 Are there any other uses for while?

 Variable number of columns can be done more elegantly than by _copying 
 pasting_ code, and it can even vary at run time:

 tr tal:repeat=row php:array_chunk(data, number_of_columns)
  td tal:repeat=col row tal:content=col /
 /tr


Thank you very much  for this sample! It's much more elegant, easier to
understand and maintain than what I've come up with. Things like this had
better be published in a special section of documentation. And I hope that
array_chunk() doesn't copy data needlessly. This method does introduce more
overhead.

Kornel, what would you do if data were a collection but not an array
(besides using something like what was suggested by Rasmus Schultz at
http://php.net/manual/en/function.array-chunk.php)?
___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


[PHPTAL] while loop in PHPTAL

2009-12-29 Thread romtek
I was looking for a more elegant way to generate grids using PHPTAL that
what I've been using and stumbled upon this post:
http://lists.motion-twin.com/pipermail/phptal/2007-February/000730.html. In
it, Aaron McClimont has proposed adding a while loop (and also
dowhile)construct to PHPTAL. I think it's very valuable as it allows for
much more elegant code in certain situations. Has this been added to the
library? If not, could it be?

Roman
___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal