Hello

Take a look at the actual RenderTool 
(http://velocity.apache.org/tools/releases/1.4/generic/RenderTool.html).

Or at the new evaluate directive, although still in developement 
(http://velocity.apache.org/engine/devel/vtl-reference-guide.html#aevaluate_-_dynamically_evaluates_a_string_or_reference).

Etienne

-----Message d'origine-----
De : Adrian Tarau [mailto:[EMAIL PROTECTED] 
Envoyé : mardi 19 février 2008 07:29
À : dev@velocity.apache.org
Objet : Call a velocity macro

Hello,

 

I have the following problem : I would like to call a macro but the macro name 
must be a variable.

 

Ex: instead of #renderLabel($component) to have #call("renderLabel"
$component) - of course "renderLabel" can be any (existing) macro name.

 

I started to create a directive #call, but of course the render method fails 
because the nodeTree is not initialized. nodeTree is initialized during parsing 
like this

 

public class ASTDirective extends SimpleNode {

 

...

           directive = rsvc.getVelocimacro( directiveName, 
context.getCurrentTemplateName());

 

            try 

            {

                directive.init( rsvc, context, this );

            }

 

..

{

 

 

Any thoughts? I think such a directive is very useful, I don't know why is not 
part of the library.

Thanks.

 

CallDirective source code : 

 

public class CallDirective extends Directive {

 

    public String getName() {

        return "call";

    }

 

    public int getType() {

        return LINE;

    }

 

    public boolean render(InternalContextAdapter context, Writer writer, Node 
node) throws IOException, ResourceNotFoundException, ParseErrorException, 
MethodInvocationException {

        if (node.jjtGetNumChildren() < 1) {

            rsvc.error("#" + getName() + " : invalid number of parameters, must 
be at least the macro name and 0..N parameters");

            return false;

        }

        String macroName = (String) node.jjtGetChild(0).value(context);

        VelocimacroProxy velocimacro = (VelocimacroProxy) 
rsvc.getVelocimacro(macroName, context.getCurrentTemplateName());

        if (velocimacro == null) {

            rsvc.error("A macro with name '" + macroName + " in context '" +
context.getCurrentTemplateName() + "' doesn't exists");

            return false;

        }

        return velocimacro.render(context, writer, node);

    }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to