【问题标题】:PHPTAL and nested templates. Possible?PHPTAL 和嵌套模板。可能的?
【发布时间】:2009-08-22 23:25:47
【问题描述】:

过去几天我一直在玩 PHPTAL。总的来说,我真的很喜欢它。它比我研究过的大多数其他人更容易进入。不过,我遇到了一个特殊问题。

这就是问题所在。我正在尝试嵌套两个模板。假设 InnerClass 有这个模板:

<div>Hello World!</div>

OuterClass 有以下模板:

<div tal:content="myVar">This text should be replaced with the HTML above.</div>

InnerClass 还有一个叫做render() 的方法,它本质上是调用themplate 的execute() 方法并返回内容。所以我在外部类中这样做:

$template->myVar = $innerClassObject->render();

然后,我显示 OuterClass 的内容。问题是内部类的渲染 HTML 被转义了,我看到 ">" 和 "

由于这种方法不起作用,嵌套 PHPTAL 模板的最佳方法是什么?我认为这是可能的,只是我缺乏知识,所以任何意见都表示赞赏。

【问题讨论】:

    标签: php templates template-tal phptal


    【解决方案1】:

    如果要在模板中插入任意标记,请使用 structure 关键字:

    <div tal:content="structure variable_that_contains_html"/>
    

    但如果您想在另一个模板中嵌入一个 PHPTAL 模板,请使用macros:

    macros.xhtml:

    <div metal:define-macro="greeting">Hello World!</div>
    

    page.xhtml:

    <body><tal:block metal:use-macro="macros.xhtml/greeting"/></body>
    

    【讨论】:

    • 太好了,这就是我要找的。显然,我在 PHPTAL 文档中从未走得那么远。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多