简体   繁体   English

您是否知道具有当前上下文节点概念的任何流行的PHP模板引擎?

[英]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.

相关问题 你知道PHP的任何好的基于文件的缓存吗? - Do you know of any good File based caching for PHP? 您是否知道任何PHP库可以编写JSON格式的日志? - Do you know any PHP library to write logs with JSON format? 何时使用 PHP 模板引擎 - When to use PHP template engines Wordpress - The7 模板 - 在每个页面上我都有 4 个警告并且不知道如何在菜单或 php 站点/内容中禁用它 - Wordpress - The7 Template - On every page I have 4 Warnings and do not know how to disable it in menu or php site/content 您是否知道PHP中有使用我可以研究的名称空间的项目? - Do you know of any projects in PHP that use namespaces that I could study? 通过链接脚本引用PHP。 我的安全吗? 你知道更好的吗? - PHP referral by link script. Is mine secure? Do you know any better ones? 您知道任何已将PHP的本机类型重新定义为对象的项目吗?这可行吗? - Do you know of any project that has redefined the native types of PHP as objects? Is it feasible? 您是否知道PHP中有任何库或代码可以检查日期是否满足给定的cron表达式? - Do you know any library or code in PHP to check if a date satisfies a given cron expression? 您如何在php中自定义登录字段以使网站类似于Facebook或其他流行的登录网站? - How do you customize the login-fields in php to make a site similar to Facebook or other popular login sites? 搜索引擎是否对 somesite.com/index.php?page=photos 这样的网址有问题 - Do search engines have trouble with urls like somesite.com/index.php?page=photos
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM