Forgot -- Re: [PHPTAL] How do you print a grid with tal:repeat ?

Levi Stanley levi at eneservices.com
Wed Feb 21 00:06:42 CET 2007


Flip, I wish I would have ran into you sooner, because I normally 
generate grids with the first method I gave you.

Also, I added one step to the last method I sent you previously.  I 
added array_chunk which you can find here in the php manual: 
http://us2.php.net/manual/en/function.array-chunk.php

Below is how I used it to generate the multi-dimensional array, we had 
in the previous example.

<?php

        include_once('PHPTAL.php');

        $array = array( 'Janet', 'Jackson' , 'Mike', 'Long' , 'Akira');

        $multi = array_chunk($array,2);

        $template = new PHPTAL('test.xhtml');

        $template->set('array',$multi);

        try {
                print $template->execute();
        } catch (Exception $e){
                print "Error: {$e->getMessage()}\n";
        }

?>


Taylor Dondich wrote:
> That is actually probably the best way of doing it.  :)  Less 
> code-like in the template.
>
> Taylor
>
> Levi Stanley wrote:
>> Another way you can do it, is if you set up a multi-dimensional array 
>> before you send it off to the xhtml.
>>
>> Here is the other way:
>>
>> <?php
>>
>>        include_once('PHPTAL.php');
>>
>>        $multi = array( array( 'Janet', 'Jackson' ),
>>                        array( 'Mike', 'Long' ),
>>                        array( 'Akira'));
>>
>>        $template = new PHPTAL('test.xhtml');
>>
>>        $template->set('array',$multi);
>>
>>        try {
>>                print $template->execute();
>>        } catch (Exception $e){
>>                print "Error: {$e->getMessage()}\n";
>>        }
>>
>> ?>
>>
>>
>> Here is the xhtml:
>>
>> <html>
>> <head>
>> </head>
>> <body>
>>        <table>
>>                <tr tal:repeat="row array">
>>                        <td tal:repeat="col row">
>>                                <span tal:replace="col" />
>>                        </td>
>>                </tr>
>>        </table>
>> </body>
>> </html>
>>
>> Doing it this way, you don't have the opportunity to use &nbsp;  but 
>> you can probably add that into your multi-dimensional array, and have 
>> a tal:condition if this value is something push out the &nbsp;
>>
>> I am not sure if this is what you were looking for?  I hope it helped.
>>
>> Best regards,
>> Levi
>>
>>
>>
>>
>> Taylor Dondich wrote:
>>> That is pretty crazy, as it requires the php modifier.  Is this 
>>> truly the most elegant way of doing this with PHPTAL?  If so, I'll 
>>> take it.  Just wanted to see if any others had any ideas?
>>>
>>> Taylor
>>>
>>> Levi Stanley wrote:
>>>> Forgot to think of the case of not even array, so fixed it in this 
>>>> version  Take a look at the xhtml:
>>>>
>>>> <html>
>>>> <head>
>>>> </head>
>>>> <body>
>>>> <table id='name'>
>>>> <tr tal:repeat="row php: range(0,(ceil(count(items)/2))-1)" >
>>>>        <td tal:repeat="col php: range(0,1)">
>>>>                <span tal:define="global x php: row*2 + col" 
>>>> tal:condition="exists: items/${x}" tal:replace="items/${x}" />
>>>>                <span tal:condition="not: exists: items/${x}" 
>>>> >&nbsp;</span>
>>>>        </td>
>>>> </tr>
>>>> </table>
>>>> </body>
>>>> </html>
>>>>
>>>>
>>>> I added the span with the &nbsp in it.  And I changed the row to 
>>>> use the ceiling fuction so you won't be missing a row.
>>>> Taylor Dondich wrote:
>>>>> Suppose I have 13 items in my collection, and I want to print a 
>>>>> table with 2 columns and as many rows as needed for my 
>>>>> collection.  How do I do that?  And if the last row only has one 
>>>>> item, how do I know to print the empty td element?
>>>>>
>>>>> Anyone have any ideas or best practices for this?
>>>>>
>>>>> Taylor
>>>>>
>>>>> _______________________________________________
>>>>> PHPTAL mailing list
>>>>> PHPTAL at lists.motion-twin.com
>>>>> http://lists.motion-twin.com/mailman/listinfo/phptal
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>>
>>>
>>>
>>
>
>
> !DSPAM:1,45db2152284721909919347!
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.motion-twin.com/pipermail/phptal/attachments/20070220/21ef9b5b/attachment.htm


More information about the PHPTAL mailing list