You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Townson, Chris" <C....@nature.com> on 2005/11/15 11:22:57 UTC

macro overloading

Hi,

Is there any way to overload macros (i.e. two or more macros with the same
name, but a different number of arguments)?

I googled "velocity macro overload" and, from the results, I'm guessing the
answer is no. :(

.... in which case, my question would be this: are there any elegant ways
people have devised to "fake it"?! :D

My first thought was to create an entirely new macro with a new name, and
call the old one from within that ... but I really want to keep the same
macro name and just add an argument (without breaking the pages where this
macro is currently being used)

Below is my old macro (to which I want to add an argument) and my rather
clunky extension to it. I'm sure this must be a fairly common problem, but I
can't think of/find anything neater.

As ever, your comments and suggestions are much appreciated.

Best wishes,

Chris

======== sample macro code below ========

#*
 * getConfig
 *
 * Macro to include configuration files: do not call this from templates.
Use #config.
 * Takes two parameters:
 *   var STRING $scope: defines the scope of the confif file (global ||
group || site)
 *   var STRING $path: sets the filepath to the config file. This is
appended with /config/$scope.config.vm. Do not include trailing slash on
folders
*#
#macro ( getConfig $scope $path )    
    #parse ( "${path}/config/${scope}.config.vm" )
#end



#*
 * config
 *
 * Macro to get and set configurations
 *
 * Takes four parameters:
 *   var STRING $siteDirectory: defines the base working directory for the
site
 *   var BOOL $includeGlobalConfig: boolean to establish whether or not to
include the global config file (true || false)
 *   var BOOL $includeGroupConfig: boolean to establish whether or not to
include a group config file (true || false)
 *   var STRING $groupDirectory: if required, defines the base directory for
group files. Use empty string if not required.
*#
#macro ( config $siteDirectory $includeGlobalConfig $includeGroupConfig
$groupDirectory )
    #if ( $includeGlobalConfig )
        #getConfig ( "global" "/common" )
    #end
    
    #if ( $includeGroupConfig )
        #getConfig ( "group" $groupDirectory )
    #end
    
    #getConfig ( "site" $siteDirectory )
#end


#*
 * extendedConfig
 *
 * Macro to get and set configurations
 *
 * This extended version of the config macro enables an additional "section"
scope
 * for setting parameters common to a subgroup of pages within a site.
 *
 * Takes six (6) parameters:
 *   var STRING $siteDirectory: defines the base working directory for the
site
 *   var BOOL $includeGlobalConfig: boolean to establish whether or not to
include the global config file (true || false)
 *   var BOOL $includeGroupConfig: boolean to establish whether or not to
include a group config file (true || false)
 *   var STRING $groupDirectory: if required, defines the base directory for
group files. Use empty string if not required.
 *   var BOOL $includeSectionConfig: boolean to establish whether or not to
include a section config file (true || false)
 *   var STRING $sectionDirectory: if required, defines the base directory
for a site section. Use empty string if not required.
*#
#macro ( extendedConfig $siteDirectory $includeGlobalConfig
$includeGroupConfig $groupDirectory $includeSectionConfig $sectionDirectory
)
    #if ( $includeGlobalConfig )
        #getConfig ( "global" "/common" )
    #end
    
    #if ( $includeGroupConfig )
        #getConfig ( "group" $groupDirectory )
    #end
    
    #getConfig ( "site" $siteDirectory )
    
    #if ( $includeSectionConfig )
        #getConfig ( "section" $sectionDirectory )
    #end
#end

********************************************************************************   
DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
not the original intended recipient. If you have received this e-mail in error
please inform the sender and delete it from your mailbox or any other storage
mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
liability for any statements made which are clearly the sender's own and not
expressly made on behalf of Macmillan Publishers Limited or one of its agents.
Please note that neither Macmillan Publishers Limited nor any of its agents
accept any responsibility for viruses that may be contained in this e-mail or
its attachments and it is your responsibility to scan the e-mail and 
attachments (if any). No contracts may be concluded on behalf of Macmillan 
Publishers Limited or its agents by means of e-mail communication. Macmillan 
Publishers Limited Registered in England and Wales with registered number 785998 
Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS   
********************************************************************************