Velocity
  1. Velocity
  2. VELOCITY-666

Blockmacro support (allows any AST as macro body argument)

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6.2, 1.7
    • Fix Version/s: 1.7
    • Component/s: None
    • Labels:
      None

      Description

      Inspired by VELOCITY-583 (BlockMacro support) I implemented the same functionality in a slightly different way.

      The new syntax is:

      #@yourMacroName($arg1 $arg2) any valid velocity AST here #end

      so basically the syntax is exactly the same as for normal macros except you put that @ prefix to the macro name. That tells Velocity that there's a macro AST body that should be passed to the actual macro.

      And in the macro you can refer to the passed body 0-N times. Like:

      #macro(yourMacroName $foo $bar)
      $bodyContent
      #end

      1. velocity-call-directive.patch
        20 kB
        Jarkko Viinamäki
      2. velocity-blockmacro.patch
        22 kB
        Jarkko Viinamäki

        Issue Links

          Activity

          Jarkko Viinamäki created issue -
          Jarkko Viinamäki made changes -
          Field Original Value New Value
          Attachment velocity-call-directive.patch [ 12397802 ]
          Jarkko Viinamäki made changes -
          Attachment velocity-blockmacro.patch [ 12398082 ]
          Nathan Bubna made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 1.7 [ 12313453 ]
          Resolution Fixed [ 1 ]
          Byron Foster made changes -
          Link This issue relates to VELOCITY-675 [ VELOCITY-675 ]
          Jarkko Viinamäki made changes -
          Summary RFC: new directive: #call Blockmacro support (allows any AST as macro body argument)
          Description Inspired by VELOCITY-583 (BlockMacro support) I implemented the same functionality in a slightly different way.

          This patch introduces a new directive #call("mymacro" $arg1 $arg2 ... ) any valid Velocity content here #end

          This directive causes a call to defined macro with given arguments AND passes the enclosed AST as an argument which can be referenced with $bodyContent (default, name is configurable).

          An example:

           #set($foobar = "yeah!")
           
           #macro(strong $txt)
           <strong>$bodyContent</strong> $txt
           #end

           #call("strong" $foobar)
           <u>This text is underlined and bold</u>
           #end
           

          Will print:

           <strong><u>This text is underlined and bold<u></strong> yeah!

          Like I commented in VELOCITY-583 the same thing can be done by first using #define to build up some custom AST and then pass that AST as an argument to some macro. While it works, it's not as convenient as this syntax. This patch however increases the amount of code lines in Velocity and the implementation is a bit "hackish" so even I'm not totally convinced whether we should commit this.

          But anyway, here it is, I'd love to hear your comments.
          Inspired by VELOCITY-583 (BlockMacro support) I implemented the same functionality in a slightly different way.

          The new syntax is:

          #@yourMacroName($arg1 $arg2) any valid velocity AST here #end

          so basically the syntax is exactly the same as for normal macros except you put that @ prefix to the macro name. That tells Velocity that there's a macro AST body that should be passed to the actual macro.

          And in the macro you can refer to the passed body 0-N times. Like:

          #macro(yourMacroName $foo $bar)
             $bodyContent
          #end
          Claude Brisson made changes -
          Resolution Fixed [ 1 ]
          Status Resolved [ 5 ] Reopened [ 4 ]
          Claude Brisson made changes -
          Status Reopened [ 4 ] Closed [ 6 ]
          Resolution Fixed [ 1 ]
          Mark Thomas made changes -
          Workflow jira [ 12450128 ] Default workflow, editable Closed status [ 12551520 ]
          Mark Thomas made changes -
          Workflow Default workflow, editable Closed status [ 12551520 ] jira [ 12552407 ]

            People

            • Assignee:
              Unassigned
              Reporter:
              Jarkko Viinamäki
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development