Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define a velocity macro to "wrap" other content?

Tags:

html

velocity

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 ?

like image 440
phtrivier Avatar asked Feb 08 '12 13:02

phtrivier


1 Answers

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.)

like image 125
Dave Newton Avatar answered Nov 05 '22 04:11

Dave Newton