Interface JxltEngine.Template

  • All Known Implementing Classes:
    TemplateScript
    Enclosing class:
    JxltEngine

    public static interface JxltEngine.Template
    A template is a JEXL script that evaluates by writing its content through a Writer.

    The source text is parsed considering each line beginning with '$$' (as default pattern) as JEXL script code and all others as Unified JEXL expressions; those expressions will be invoked from the script during evaluation and their output gathered through a writer. It is thus possible to use looping or conditional construct "around" expressions generating output.

    For instance:
    
     $$ for (var x : [1, 3, 5, 42, 169]) {
     $$   if (x == 42) {
     Life, the universe, and everything
     $$   } else if (x > 42) {
     The value $(x} is over forty-two
     $$   } else {
     The value ${x} is under forty-two
     $$   }
     $$ 
     }

    Will evaluate as:

     The value 1 is under forty-two
     The value 3 is under forty-two
     The value 5 is under forty-two
     Life, the universe, and everything
     The value 169 is over forty-two
     

    During evaluation, the template context exposes its writer as '$jexl' which is safe to use in this case. This allows writing directly through the writer without adding new-lines as in:

     $$ for(var cell : cells) { $jexl.print(cell); $jexl.print(';') }
     

    A template is expanded as one JEXL script and a list of template expressions; each template expression is being replaced in the script by a call to jexl:print(expr) (the expr is in fact the expr number in the template). This integration uses a specialized JexlContext (TemplateContext) that serves as a namespace (for jexl:) and stores the template expression array and the writer (java.io.Writer) that the 'jexl:print(...)' delegates the output generation to.

    Since:
    3.0
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.lang.String asString()
      Recreate the template source from its inner components.
      void evaluate​(JexlContext context, java.io.Writer writer)
      Evaluates this template.
      void evaluate​(JexlContext context, java.io.Writer writer, java.lang.Object... args)
      Evaluates this template.
      java.lang.String[] getParameters()
      Gets the list of parameters expected by this template.
      java.util.Map<java.lang.String,​java.lang.Object> getPragmas()
      Gets this script pragmas.
      java.util.Set<java.util.List<java.lang.String>> getVariables()
      Gets the list of variables accessed by this template.
      JxltEngine.Template prepare​(JexlContext context)
      Prepares this template by expanding any contained deferred TemplateExpression.
      default JxltEngine.Template prepare​(JexlContext context, java.lang.Object... args)
      Prepares this template by expanding any contained deferred TemplateExpression with optional arguments.
    • Method Detail

      • asString

        java.lang.String asString()
        Recreate the template source from its inner components.
        Returns:
        the template source rewritten
      • evaluate

        void evaluate​(JexlContext context,
                      java.io.Writer writer)
        Evaluates this template.
        Parameters:
        context - the context to use during evaluation
        writer - the writer to use for output
      • evaluate

        void evaluate​(JexlContext context,
                      java.io.Writer writer,
                      java.lang.Object... args)
        Evaluates this template.
        Parameters:
        context - the context to use during evaluation
        writer - the writer to use for output
        args - the arguments
      • getParameters

        java.lang.String[] getParameters()
        Gets the list of parameters expected by this template.
        Returns:
        the parameter names array
      • getPragmas

        java.util.Map<java.lang.String,​java.lang.Object> getPragmas()
        Gets this script pragmas.
        Returns:
        the (non-null, possibly empty) pragmas map
        Since:
        3.1
      • getVariables

        java.util.Set<java.util.List<java.lang.String>> getVariables()
        Gets the list of variables accessed by this template.

        This method will visit all nodes of the sub-expressions and extract all variables whether they are written in 'dot' or 'bracketed' notation. (a.b is equivalent to a['b']).

        Returns:
        the set of variables, each as a list of strings (ant-ish variables use more than 1 string) or the empty set if no variables are used
      • prepare

        JxltEngine.Template prepare​(JexlContext context)
        Prepares this template by expanding any contained deferred TemplateExpression.
        Parameters:
        context - the context to prepare against
        Returns:
        the prepared version of the template
      • prepare

        default JxltEngine.Template prepare​(JexlContext context,
                                            java.lang.Object... args)
        Prepares this template by expanding any contained deferred TemplateExpression with optional arguments.

        This binds arguments to template parameters for immediate expressions when the template also uses deferred/nested expressions.

        Parameters:
        context - the context to prepare against
        args - the arguments to bind (optional)
        Returns:
        the prepared version of the template
        Since:
        3.6.2