tags:

views:

44

answers:

2

This question is related to this one http://stackoverflow.com/questions/3940927/is-there-anything-like-dwoo-s-with-or-loop-in-smarty-3-or-earlier

Basically I want to have something like current node from XSLT templates.

In XSLT when I write something like:

<xsl:for-each select="catalog/cd">
<tr>
  <td><xsl:value-of select="title"/></td>
  <td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>

artist actually refers to catalog/cd[1]/artist (and of course [2],[3] ... and so on if there are more cd-s)

Current context in which field names are understood changes inside for-each block.

I very much like this functionality. Do you know any popular PHP template engine (other than Dwoo) that has this functionality?

UPDATE:

Tim Fountain suggested:

// smarty
{foreach from=$cds item=cd}
    <tr>
        <td>{$cd->title}</td>
        <td>{$cd->artist}</td>
    </tr>
{/foreach}

but I'd prefer something like:

// dwoo
{foreach from=$cds}
    <tr>
        <td>{$title}</td>
        <td>{$artist}</td>
    </tr>
{/foreach}

which will not work.

Think about nested loop (let's assume cd has multiple artists):

// smarty
{foreach from=$cds item=cd}
    <tr>
        <td>{$cd->title}</td>
        <td><ul>
        {foreach from=$cd->artist item=$ar}
           <li>{$ar}</li>
        {/foreach}
        </ul></td>
    </tr>
{/foreach}

when I'd prefer

// dwoo
{foreach from=$cds}
    <tr>
        <td>{$title}</td>
        <td><ul>
        {foreach from=$artist}
           <li>{$}</li>
        {/foreach}
        </ul></td>
    </tr>
{/foreach}

Also If I also have a collection of music on cassettes I can iterate over it with same code:

// dwoo
{foreach from=$mcs}
    <tr>
        <td>{$title}</td>
        <td><ul>
        {foreach from=$artist}
           <li>{$}</li>
        {/foreach}
        </ul></td>
    </tr>
{/foreach}

I don't know if I could use same name for loop variable over and over like here:

// smarty, buggy?
{foreach from=$mcs item=o}
    <tr>
        <td>{$o->title}</td>
        <td><ul>
        {foreach from=$o->artist item=o}
           <li>{$o}</li>
        {/foreach}
        </ul></td>
        <td>{$o->title}</td>
    </tr>
{/foreach}

But I suppose inner $o would have overwritten outer $o

+1  A: 

PHP itself?

If I understand correctly you want this functionality

<?php foreach($catalog as $cd): ?>
    <tr>
        <td><?php echo $cd['title'] ?>
        <td><?php echo $cd['artist'] ?>
    </tr>
<?php endforeach ?>

You can have this on its own inside a template file and have your controller create the $catalog array and pass it to the template.

Manos Dilaverakis
Yeah. Almost that but without $cd variable. ;-)
Kamil Szot
@Kamil most of PHP scripts do get information with database tables, not XML files. So, you have to get this catalog from database anyway. In that rare case when you have in in XML file, it's just one command to get it into array.
Col. Shrapnel
@Col. I've been misunderstood. I gave XSLT as example of templating engine (fact that it's templating engine for XML not for PHP is not important) that has the feature I want (current context). I seek same feature but in templating engine for PHP. Please take a look at http://wiki.dwoo.org/index.php/Blocks:loop and http://wiki.dwoo.org/index.php/Blocks:with and compare it to how smarty templates are usually used.
Kamil Szot
@Kamil XSLT can be used in PHP as well. But "current context" is XML feature. Isn't it belongs to source data?
Col. Shrapnel
@Col. Yes. I know. But XSLT is not especially comfortable template engine to use. Current context is definitely associated with the template not the data. It's XSLT feature not XML feature. Similarly it's Dwoo feature not PHP array feature. I wan't to know if there is another template engine for PHP with this feature or whether there exists Smarty 3 plugin that implements this feature.
Kamil Szot
A: 

Is this not just like a normal foreach loop? So for example in Smarty the equivalent would be:

{foreach from=$cds item=cd}
    <tr>
        <td>{$cd->title}</td>
        <td>{$cd->artist}</td>
    </tr>
{/foreach}

but all the templating options, and PHP itself, can do this.

Tim Fountain
Almost that but without need to invent name for item= and using it later explicitly. Please read updated question.
Kamil Szot