XF 2.1 Macro layouts with child content

peregm

Member
Hi there!

I really didn't know how to phrase this, so maybe the title does not have any sense at all, but here is what I'm meaning:
Is it possible to have a macro that includes the content specified between the macro tags? Here's an example:

Assume I have this macro defined in my test_macros template:
XML:
<xf:macro name="macro1">
    <h1 class="title">Some title</h1>
    <div class="content">
        {{$content}}
    </div>
</xf:macro>

And in some other template, I want to replace the $content variable with all the code that is inserted inside the macro call:
XML:
<xf:macro template="test_macros" name="macro1">
    <span>Some content</span>
    <p>Some other content...</p>
</xf:macro>

<xf:macro template="test_macros" name="macro1">
    <span>Some other content</span>
    <ul>
        <li>Foo</li>
        <li>Bar</li>
        <li>Baz</li>
    </ul>
</xf:macro>

So, the actual content that will be rendered in my page would be:
HTML:
<h1 class="title">Some title</h1>
<div class="content">
    <span>Some content</span>
    <p>Some other content...</p>
</div>


<h1 class="title">Some title</h1>
<div class="content">
    <span>Some other content</span>
    <ul>
        <li>Foo</li>
        <li>Bar</li>
        <li>Baz</li>
    </ul>
</div>

I know I can actually pass variables to my macro using the arg-varName tag, but what I do really want is to pass an entire block of code/template/macro to one of these variables.
Can I achieve something similar to this using some template syntax?

Thanks!
 
Okay, answering my own question because I've actually found a working solution.
What I did is enclose my content inside an <xf:set> tag and simply passed the variable to my macro. I really didn't know you could do that!

So here's what I finally have, in case anyone wonders:
XML:
<xf:set var="$content">
    <span>Some content</span>
    <p>Some other content...</p>
</xf:set>
<xf:macro template="test_macros" name="macro1" arg-content="{$content}" />


<xf:set var="$content">
    <span>Some other content</span>
    <ul>
        <li>Foo</li>
        <li>Bar</li>
        <li>Baz</li>
    </ul>
</xf:set>
<xf:macro template="test_macros" name="macro1" arg-content="{$content}" />

And in my macro, I put my $content argument:
XML:
<xf:macro name="macro1" arg-content="!">
    <h1 class="title">Some title</h1>
    <div class="content">
        {{$content}}
    </div>
</xf:macro>

And with that I just got the output I wanted!
 
Top Bottom