Class RuntimeMacro

java.lang.Object
org.apache.velocity.runtime.directive.Directive
org.apache.velocity.runtime.directive.RuntimeMacro
All Implemented Interfaces:
Cloneable, DirectiveConstants

public class RuntimeMacro extends Directive
This class acts as a proxy for potential macros. When the AST is built this class is inserted as a placeholder for the macro (whether or not the macro is actually defined). At render time we check whether there is a implementation for the macro call. If an implementation cannot be found the literal text is rendered.
Since:
1.6
  • Field Details

    • strictRef

      protected boolean strictRef
      Indicates if we are running in strict reference mode.
  • Constructor Details

    • RuntimeMacro

      public RuntimeMacro()
  • Method Details

    • getName

      public String getName()
      Return name of this Velocimacro.
      Specified by:
      getName in class Directive
      Returns:
      The name of this Velocimacro.
    • getScopeName

      public String getScopeName()
      Override to always return "macro". We don't want to use the macro name here, since when writing VTL that uses the scope, we are within a #macro call. The macro name will instead be used as the scope name when defining the body of a BlockMacro.
      Overrides:
      getScopeName in class Directive
      Returns:
      the name to be used when a scope control is provided for this directive.
    • getType

      public int getType()
      Velocimacros are always LINE type directives.
      Specified by:
      getType in class Directive
      Returns:
      The type of this directive.
    • init

      public void init(RuntimeServices rs, String name, InternalContextAdapter context, Node node)
      Initialize the Runtime macro. At the init time no implementation so we just save the values to use at the render time.
      Parameters:
      rs - runtime services
      name - macro name
      context - InternalContextAdapter
      node - node containing the macro call
    • render

      Velocimacro implementation is not known at the init time. So look for an implementation and if finds one renders it. The actual rendering is delegated to the VelocimacroProxy object. A call asks, in this order: the macros of the template it is written in; if inline macros replace global ones, those of the other templates of the merge, the latest entered first; the global macros; the other templates of the merge, the latest entered first. The templates of a merge are the libraries it was given, its root template and what was #parsed so far. A template only has macros of its own under velocimacro.inline.local_scope.
      Specified by:
      render in class Directive
      Parameters:
      context -
      writer -
      node -
      Returns:
      true if the rendering is successful
      Throws:
      IOException
      ResourceNotFoundException
      ParseErrorException
      MethodInvocationException
    • render

      This method is used with BlockMacro when we want to render a macro with a body AST.
      Parameters:
      context -
      writer -
      node -
      body - AST block that was enclosed in the macro body.
      Returns:
      true if the rendering is successful
      Throws:
      IOException
      ResourceNotFoundException
      ParseErrorException
      MethodInvocationException