[PHPTAL] Array Syntax Question

Levi Stanley levi at eneservices.com
Mon Feb 19 01:49:00 CET 2007


I think I see your dilemma.  But, the way you put it is kind confusing.

So let me take your first  array and try to explain something about how 
PHPTAL works on them.

Lets take:

$fooArr = array('bazKey' => array('item1', 'item2', 'item3'));

Let me give you some code:

<?php

       include_once('PHPTAL.php');

       $fooArr = array('bazKey' => array('item1', 'item2', 'item3'));
       $template = new PHPTAL('test2.xhtml');
       $template->set('array', $fooArr);

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

?>

This is the xHtml file I am using for this example:

<html>
<head>
</head>
<body>
<div tal:repeat="item array">
       <div tal:repeat="nested_item item" tal:content="nested_item"/>
</div>
</body>
</html>

Okay with that done lets break up how I was able to get to the 
information of the multi-dimensional array.

<div tal:repeat="item array">

This statement allows me to get access to the value of the value pairs, 
which is held by BazKey.  I guess here I could probably say, keys are 
pretty much useless when your array is set up like this, however, there 
are ways to make it make sense, which I will show you in another 
example.  But back to this example.

Now the final, nested array is access with this statement:

<div tal:repeat="nested_item item" tal:content="nested_item"/>

The output you will get is this:

<html>
<head>
</head>
<body>
<div>
       <div>item1</div><div>item2</div><div>item3</div></div></body>
</html>


Now for a more complex example:

Here is the php code:

<?php

       include_once('PHPTAL.php');

       $array = array( array('position' => 'Management', 'personel' => 
array('Billy', 'Mandy')),
                       array('position' => 'Programmers', 'personel' => 
array('Kevin', 'Jimmy', 'Tucker'))
       );

       $template = new PHPTAL('test.xhtml');
       $template->set('array', $array);

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

       }

?>

Here is the xhtml file:

<html>
<head>
</head>
<body>
       <span id="table_css"tal:repeat="multi_array array">
               <div id="title_css" tal:content="multi_array/position"/>
               <div id="employee_css" tal:repeat="person 
multi_array/personel" tal:content="person" />
       </span>
</body>
</html>

The output of this script is this:

<html>
<head>
</head>
<body>
       <span id="table_css">
               <div id="title_css">Management</div>
               <div id="employee_css">Billy</div><div 
id="employee_css">Mandy</div>    </span><span id="table_css">
               <div id="title_css">Programmers</div>
               <div id="employee_css">Kevin</div><div 
id="employee_css">Jimmy</div><div id="employee_css">Tucker</div> </span>
</body>
</html>

I hope I have answered your question.

Best regards,

Levi

Werner wrote:
> Hi Again...
>
> (if anyone is still reading this thread :-) )
>
> Pardon, it *does* work with a simple array. I just realized that I found
> this problem not by dealing with a simple array, but with a
> multi-dimensional array - that's how this whole syntax-problem got
> started during my current project.
>
> For reference, Joshua's example syntax *does* work for the example in my
> previous message, when applied like this:
>    tal:attributes="class barArr/${repeat/item/index}"
>
> I would like to adjust my question example to the following:
>
> PHP:
>    $fooArr = array('bazKey' => array('item1', 'item2', 'item3'));
>    $barArr = array('class1', 'class2', 'class3');
>
> Now, $fooArr is multi-dimensional and I cannot get it to work. This is
> the real problem I an still trying to solve...
>
> Here is the updated question template source that does not work:
>
> PHPTAL:
>    tal:attributes="class barArr/bazKey/${repeat/item/index}"
>
> Or:
>    tal:attributes="class ${barArr/bazKey}/${repeat/item/index}"
>
> Or:
>    tal:attributes="class barArr/${bazKey}/${repeat/item/index}"
>
> More simplified:
>    tal:attributes="class barArr[bazKey[repeat/item/index]]"
>
> By the way, it seems to me that it is not possible to nest "${}"'s
> inside each other - is this true?
>
> Please indicate how I would go about solving this syntax dilemma :-)
>
> Thanks again,
> Werner
>
> Werner wrote:
>> Hi Laurent
>>
>> Indeed I tried Joshua's solution (as you explained), but still cannot 
>> get it to work. I think I am missing something simple here?
>>
>> Here is a simple example of what I would like to do:
>>
>> PHP:
>>    $fooArr = array('item1', 'item2', 'item3');
>>    $barArr = array('class1', 'class2', 'class3');
>>
>> $fooArr holds the items i would like to iterate over.
>> $barArr holds the css classes that have to be assigned to each item. 
>> Each item in $fooArr should be assigned the class of $barArr of the 
>> corresponding array index.
>>
>> Here follows the template source (that does not work):
>>
>> PHPTAL:
>>    <span tal:repeat="item fooArr" tal:omit-tag="">
>>        <p tal:content="item" tal:attributes="class 
>> barArr[repeat/item/index]">Some Foo Item</p>
>>    </span>
>>
>> Please provide an example of how to make this work using the  
>> "${...}" syntax.  Maybe I'm just too tired, but I cannot make it work.
>>
>> Thanks for all your time and effort,
>> Werner
>>
>>
>> lbedubourg wrote:
>>> Werner wrote:
>>>> Hi again -
>>>>
>>>> Unfortunately none of your suggestions worked. 
>>>
>>> Hi Werner,
>>>
>>> You did not try Joshua's solution, it is the right notation.
>>>
>>> When you write a tales path "foo/bar/baz", you abstract the type of 
>>> each  path component.
>>>
>>> I you want to make one part of the path dynamic, you have to use 
>>> insert some ${foo} inside it :
>>>
>>> foo/${someKey}/baz
>>>
>>> Where 'foo' may be an array, an associative array, an object, ...
>>>
>>> Regards
>>> Laurent
>>>
>>
>>
>> _______________________________________________
>> PHPTAL mailing list
>> PHPTAL at lists.motion-twin.com
>> http://lists.motion-twin.com/mailman/listinfo/phptal
>>
>
>
>
> _______________________________________________
> PHPTAL mailing list
> PHPTAL at lists.motion-twin.com
> http://lists.motion-twin.com/mailman/listinfo/phptal
>
> !DSPAM:1,45d5e398139327836945438!
>




More information about the PHPTAL mailing list