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 157201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:27:43+00:00 2026-05-11T10:27:43+00:00

I have a macro taking several parameters. Some of these are optional and if

  • 0

I have a macro taking several parameters. Some of these are optional and if a parameter is left empty it will replaced with default.

Now the question is how to make this as easy as possible for ordinary web designer. Is there any other possibity apart from my examples to handle this case?

Example 1:

The obvious problem here is the optional values.

#macro (myTag $param1 $param2 $param3) ... #end 

Example 2:

And here the problem is a possible issue when same macro is used more than once and all variables are not set again.

#set ($param1='value1')  #set ($param2='value2')  #set ($param3='value3')   #macro (myTag) ... #end 
  • 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. 2026-05-11T10:27:44+00:00Added an answer on May 11, 2026 at 10:27 am

    As of Velocity 1.6, optional or named parameters are not supported. There was a recent patch submitted with this feature so we might see it available in a future release.

    In the meantime, consider passing in a list or a map of values. For example you can pass in a map of params as follows (requires Velocity 1.5 or greater):

    #macro(myMacro $p)   item 1: $p.param1   item 2: $p.param2 #end  #set($params = {'param1' : 'val1', 'param2':'val2'}) #myMacro($params) 

    displays:

    item 1: val1 item 2: val2 

    To handle optional parameters, use an #if within the macro to check for the parameter. Adding new elements to the map is a little messy. Since the Java method ‘put’ returns a value, you have to use #set to dispose of the return value. (Otherwise it’s displayed in the resulting text).

    #macro(myMacro $p)   #if(!$p.param1)#set($dummy = $p.put('param1', 'default1'))#end   #if(!$p.param2)#set($dummy = $p.put('param2', 'default2'))#end   #if(!$p.param3)#set($dummy = $p.put('param3', 'default3'))#end    item 1: $p.param1   item 2: $p.param2   item 3: $p.param3 #end  #set($params = {'param1' : 'val1', 'param2':'val2'}) #myMacro($params) 

    displays

    item 1: val1 item 2: val2 item 3: default3 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to have a macro that will do some formatting. I need
Right now I have a macro PopulateYearlyValues But it seems to me it's taking
I have a macro that iterates through some rows, to update the colouring of
I have a static empty chart in Excel, I have a macro which pulls
I have seen in some stackoverflow questions/answers people talking about a boost macro (I
I have a macro setup to automatically open/save a file that I am opening
I have a macro that passes the line number and file name to an
I have a macro to write, quite simple. It just pulls numeric values from
I have a macro definition in header file like this: // header.h ARRAY_SZ(a) =
If you have a macro like this in C: #define SLICE_VERSION 20110614 How to

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.