dtpfia3334 2010-10-15 12:03
浏览 63
已采纳

你知道PHP的任何流行的PHP模板引擎有当前上下文节点的概念吗?

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

  • 写回答

4条回答 默认 最新

  • douyi02577 2010-12-28 16:39
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据