phptemplatestemplate-talphptal

PHPTAL and nested templates. Possible?


I've been playing around with PHPTAL for the last couple of days. Overall I really like it. It's been much easier to get into than most others I've looked into. I am having one particular problem, though.

Here's the issue. I am trying to nest two templates. Let's say InnerClass has this template:

<div>Hello World!</div>

OuterClass has the following template:

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

InnerClass also has a method called render(), which essentially calls themplate's execute() method and returns the content. So I do this in the outer class:

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

I, then, display the content of the OuterClass. The problem is that the rendered HTML of the inner class comes escaped and I see ">" and "<" instead of actual tags. It seems that myVar is completely escaped before its content is displayed.

Since this approach does not work, what is the best way to nest PHPTAL templates? I assume it's possible and it's just lack of knowledge on my end, so any input is appreciated.


Solution

  • If you want to insert arbitrary markup in a template, then use structure keyword:

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

    but if you want to embed one PHPTAL template in another, then use macros:

    macros.xhtml:

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

    page.xhtml:

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