Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:08:19+00:00 2026-06-11T18:08:19+00:00

I’ve got a couple Velocity macros like this: #macro(Alpha) #set($p = 1) #@Beta() $p

  • 0

I’ve got a couple Velocity macros like this:

#macro(Alpha)
  #set($p = 1)
  #@Beta()
    $p             // 1
    $bodyContent
  #end
#end

#macro(Beta $params)
  #set($p = 2)
  $p               // 2
  $bodyContent
#end

And I’m using them like so:

#set($p = 0)
#@Alpha(...)
  $p               // 3
#end

I believe this renders like so (ignore formatting): 2, 2, 2

But I’d like to have proper closure behavior, including more locally scoped names hiding parent scope names. In particular, the usage of $p labelled ‘3’ should refer to the value 0, ‘2’ to the value 2, and ‘1’ to the value 1.

Given proper closure semantics, it would print: 2, 1, 0

Is there any way to get this, or a way to implement a custom directive / modify #macro directive behavior to achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:08:20+00:00Added an answer on June 11, 2026 at 6:08 pm

    Velocity is a templating engine, not a proper programming language, so it’s a bit harder to grasp how it works.

    Macros are not functions like in Java or C, meaning that invoking a macro isn’t going to create a new segment on the stack for local variables; velocity works with a context, and most of the time there’s just one global context.

    Still, there are two ways of dealing with local variables:

    1. There’s the velocimacro.context.localscope configuration parameter that prevents changing global variables from within macros; note that this setting is deprecated and will be removed in Velocity 2.0
    2. You can use the $macro variable as a local container for private variables if you enable the macro.provide.scope.control configuration parameter

    Still, there’s another problem that would prevent your code from running correctly: Velocity macros work mostly as call-by-macro expansion, which means that the body passed to a macro isn’t evaluated first, then passed to the macro; it will be evaluated dynamically when the nested macro is executed. The code behaves as:

    #macro(Alpha)
      #set($macro.p = 1)  -> $macro refers to Alpha
      $p -> $p will always be 0, since we're changing a local variable
      $macro.p  -> 1 here
      $bodyContent  -> Here $p is used, and 0 is printed
      #@Beta()
        $macro.p  -> it is 1 now, but when this line will be actually evaluated, $macro will refer to Beta, so 2 will be printed
        $bodyContent  -> It's the content of the Beta macro, which triggers an infinite recursion; it's not $p as above
      #end
    #end
    
    #macro(Beta)
      #set($macro.p = 2)  -> $macro refers to Beta
      $macro.p  -> 2 here
      $bodyContent  -> the $bodyContent that was passed into the $bodyContent is evaluated now, so it causes a recursion
    #end
    
    #set($p = 0)
    #@Alpha()
      $p  -> Global variable
    #end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.