11

I'm trying to abstract a common scenario in generated markup where I need a couple of tags to "wrap" an arbitrary content. So instead of writing this

<div class="container">
   <p class="someClass">Some header</p>
   <div id="foo">
     <!-- The real content that changes -->
   </div>
</div>

I would be able to write something "like"

#????
 <!-- The real content that changes
#end

Where obviously I don't know what the #???? would be.

As far as I know it is not possible to do this with macros, short of defining a macro for the start of the block and a macro for the end of the block.

#macro(startContained)
<div class="container">
   <p class="someClass">Some header</p>
   <div id="foo">
#end

#macro(endContained)
   </div>
</div>
#end

#startContained
<!-- The real content -->
#endContained

Any better way to do this ?

phtrivier
  • 13,047
  • 6
  • 48
  • 79

1 Answers1

13

Use the #@ macro call syntax, along with the $!bodyContent variable:

#macro(wrapper)
  <div class="container">
    <p class="someClass">Some header</p>
    <div id="foo">
      $!bodyContent##
    </div>
  </div>
#end

#@wrapper()
  The real content that changes.
#end

#@wrapper()
  Other different content.
#end

Renders as:

<div class="container">
  <p class="someClass">Some header</p>
  <div id="foo">
    The real content that changes.
  </div>
</div>

<div class="container">
  <p class="someClass">Some header</p>
  <div id="foo">
    Other different content.
  </div>
</div>

(The ## in the macro body removes trailing whitespace; for HTML it may not matter.)

Dave Newton
  • 158,873
  • 26
  • 254
  • 302