0

This question is related to this one 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

Community
  • 1
  • 1
Kamil Szot
  • 17,436
  • 6
  • 62
  • 65

4 Answers4

1

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
  • 5,849
  • 4
  • 31
  • 57
  • Yeah. Almost that but without $cd variable. ;-) – Kamil Szot Oct 15 '10 at 13:16
  • @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. – Your Common Sense Oct 15 '10 at 13:20
  • @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 Oct 15 '10 at 20:00
  • @Kamil XSLT can be used in PHP as well. But "current context" is XML feature. Isn't it belongs to source data? – Your Common Sense Oct 15 '10 at 20:23
  • @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 Oct 16 '10 at 10:05
0

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
  • 33,093
  • 5
  • 41
  • 69
  • Almost that but without need to invent name for item= and using it later explicitly. Please read updated question. – Kamil Szot Oct 15 '10 at 13:17
0

IMHO it is more confusing to omit the "invented" name, as there is no reference to what scope the data is coming from. It's much like the terribly abused extract() function in PHP that extracts an array to the current symbol table, which makes for very hard to manage code.

joe
  • 69
  • 1
  • I hate polluting namespace with extract() but I really miss the feature I asked about. I was using my own template engine for years just to have it. It was main reason why I could flexibly reuse parts of my old templates in different contexts and combinations. – Kamil Szot Dec 08 '10 at 16:01
0

As far as I know the only fairly popular open source template engine with this feature is Dwoo

Kamil Szot
  • 17,436
  • 6
  • 62
  • 65