[英]Do you know of any popular PHP template engines for php that have concept of current context node?
This question is related to this one Is there anything like Dwoo-s {with} or {loop} in Smarty 3 or earlier? 这个问题与此有关。Smarty 3或更早版本中是否有Dwoo-s {with}或{loop}之类的东西?
Basically I want to have something like current node from XSLT templates. 基本上,我想从XSLT模板获得类似当前节点的东西。
In XSLT when I write something like: 在XSLT中,当我编写如下内容时:
<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) 艺术家实际上是指目录/ cd [1] /艺术家(当然还有[2],[3] ...,如果还有更多cd-s,依此类推)
Current context in which field names are understood changes inside for-each block. for-each块内部更改了了解字段名称的当前上下文。
I very much like this functionality. 我非常喜欢此功能。 Do you know any popular PHP template engine (other than Dwoo) that has this functionality?
您是否知道具有此功能的任何流行的PHP模板引擎(Dwoo除外)?
UPDATE: 更新:
Tim Fountain suggested: Tim Fountain建议:
// 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): 考虑一下嵌套循环(假设cd有多个艺术家):
// 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 但我想内部$ o会覆盖外部$ o
PHP itself? PHP本身?
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. 您可以在模板文件中单独使用它,并让您的控制器创建$ catalog数组并将其传递给模板。
Is this not just like a normal foreach loop? 这不仅仅像普通的foreach循环吗? So for example in Smarty the equivalent would be:
因此,例如在Smarty中,等效项为:
{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. 但是所有模板选项以及PHP本身都可以做到这一点。
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.
这很像PHP中滥用严重的extract()函数,该函数将数组提取到当前符号表中,这使得管理代码非常困难。
据我所知,唯一具有此功能的相当流行的开源模板引擎是Dwoo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.