Class BaseTemplate

    • Method Detail

      • getModel

        public Map getModel()
      • run

        public abstract Object run()
      • yieldUnescaped

        public BaseTemplate yieldUnescaped​(Object obj)
                                    throws IOException
        Renders the object provided as parameter using its Object.toString() method, The contents is rendered as is, unescaped. This means that depending on what the Object.toString() method call returns, you might create invalid markup.
        Parameters:
        obj - the object to be rendered unescaped
        Returns:
        this template instance
        Throws:
        IOException
      • yield

        public BaseTemplate yield​(Object obj)
                           throws IOException
        Renders the object provided as parameter using its Object.toString() method, The contents is rendered after being escaped for XML, enforcing valid XML output.
        Parameters:
        obj - the object to be rendered
        Returns:
        this template instance
        Throws:
        IOException
      • comment

        public BaseTemplate comment​(Object cs)
                             throws IOException
        Renders the supplied object using its Object.toString() method inside a comment markup block (<!-- ... -->). The object is rendered as is, unescaped.
        Parameters:
        cs - the object to be rendered inside an XML comment block.
        Returns:
        this template instance.
        Throws:
        IOException
      • pi

        public BaseTemplate pi​(Map<?,​?> attrs)
                        throws IOException

        Renders processing instructions. The supplied map contains all elements to be rendered as processing instructions. The key is the name of the element, the value is either a map of attributes, or an object to be rendered directly. For example:

        pi("xml-stylesheet":[href:"mystyle.css", type:"text/css"])

        will be rendered as:

             <?xml-stylesheet href='mystyle.css' type='text/css'?>
         
        Parameters:
        attrs - the attributes to render
        Returns:
        this template instance
        Throws:
        IOException
      • methodMissing

        public Object methodMissing​(String tagName,
                                    Object args)
                             throws IOException
        This is the main method responsible for writing a tag and its attributes. The arguments may be:
        • a closure
        • in which case the closure is rendered inside the tag body
        • a string
        • , in which case the string is rendered as the tag body
        • a map of attributes
        • in which case the attributes are rendered inside the opening tag

        or a combination of (attributes,string), (attributes,closure)

        Parameters:
        tagName - the name of the tag
        args - tag generation arguments
        Returns:
        this template instance
        Throws:
        IOException
      • includeEscaped

        public void includeEscaped​(String templatePath)
                            throws IOException
        Includes contents of another file, not as a template but as escaped text.
        Parameters:
        templatePath - the path to the other file
        Throws:
        IOException
      • includeUnescaped

        public void includeUnescaped​(String templatePath)
                              throws IOException
        Includes contents of another file, not as a template but as unescaped text.
        Parameters:
        templatePath - the path to the other file
        Throws:
        IOException
      • tryEscape

        public Object tryEscape​(Object contents)
        Escapes the string representation of the supplied object if it derives from CharSequence, otherwise returns the object itself.
        Parameters:
        contents - an object to be escaped for XML
        Returns:
        an escaped string, or the object itself
      • getOut

        public Writer getOut()
        Convenience method to return the current writer instance.
        Returns:
        the current writer
      • layout

        public Object layout​(Map model,
                             String templateName)
                      throws IOException,
                             ClassNotFoundException
        Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting. This works similarily to a template include but allows a distinct model to be used. This version doesn't inherit the model from the parent. If you need model inheritance, see layout(java.util.Map, String, boolean).
        Parameters:
        model - model to be passed to the template
        templateName - the name of the template to be used as a layout
        Returns:
        this template instance
        Throws:
        IOException
        ClassNotFoundException
      • layout

        public Object layout​(Map model,
                             String templateName,
                             boolean inheritModel)
                      throws IOException,
                             ClassNotFoundException
        Imports a template and renders it using the specified model, allowing fine grained composition of templates and layouting. This works similarily to a template include but allows a distinct model to be used. If the layout inherits from the parent model, a new model is created, with the values from the parent model, eventually overridden with those provided specifically for this layout.
        Parameters:
        model - model to be passed to the template
        templateName - the name of the template to be used as a layout
        inheritModel - a boolean indicating if we should inherit the parent model
        Returns:
        this template instance
        Throws:
        IOException
        ClassNotFoundException
      • contents

        public Closure contents​(Closure cl)
        Wraps a closure so that it can be used as a prototype for inclusion in layouts. This is useful when you want to use a closure in a model, but that you don't want to render the result of the closure but instead call it as if it was a specification of a template fragment.
        Parameters:
        cl - the fragment to be wrapped
        Returns:
        a wrapped closure returning an empty string
      • writeTo

        public Writer writeTo​(Writer out)
                       throws IOException
        Main method used to render a template.
        Specified by:
        writeTo in interface Writable
        Parameters:
        out - the Writer to which this Writable should output its data.
        Returns:
        a writer instance
        Throws:
        IOException