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

Taylor Dondich tdondich at groundworkopensource.com
Tue Feb 20 16:56:44 CET 2007


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,45da4dda265719057474034!
>>
>




More information about the PHPTAL mailing list