Class HtmlEscape

  • All Implemented Interfaces:
    TemplateModel, TemplateTransformModel

    public class HtmlEscape
    extends java.lang.Object
    implements TemplateTransformModel
    Performs an HTML escape of a given template fragment. Specifically, < > " and & are all turned into entities.

    Usage:
    From java:

     SimpleHash root = new SimpleHash();
    
     root.put( "htmlEscape", new freemarker.template.utility.HtmlEscape() );
    
     ...
     

    From your FreeMarker template:

    
     The following is HTML-escaped:
     <transform htmlEscape>
       <p>This paragraph has all HTML special characters escaped.</p>
     </transform>
    
     ...
     
    See Also:
    XmlEscape
    • Constructor Summary

      Constructors 
      Constructor Description
      HtmlEscape()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.io.Writer getWriter​(java.io.Writer out, java.util.Map args)
      Returns a writer that will be used by the engine to feed the transformation input to the transform.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HtmlEscape

        public HtmlEscape()
    • Method Detail

      • getWriter

        public java.io.Writer getWriter​(java.io.Writer out,
                                        java.util.Map args)
        Description copied from interface: TemplateTransformModel
        Returns a writer that will be used by the engine to feed the transformation input to the transform. Each call to this method must return a new instance of the writer so that the transformation is thread-safe.

        This method should not throw RuntimeException, nor IOException that wasn't caused by writing to the output. Such exceptions should be catched inside the method and wrapped inside a TemplateModelException. (Note that setting Configuration.setWrapUncheckedExceptions(boolean) to true can mitigate the negative effects of implementations that throw RuntimeException-s.)

        Specified by:
        getWriter in interface TemplateTransformModel
        Parameters:
        out - the character stream to which to write the transformed output
        args - the arguments (if any) passed to the transformation as a map of key/value pairs where the keys are strings and the arguments are TemplateModel instances. This is never null. (If you need to convert the template models to POJOs, you can use the utility methods in the DeepUnwrap class. Though it's recommended to work with TemplateModel-s directly.)
        Returns:
        The Writer to which the engine will write the content to transform, or null if the transform does not support nested content (body). The returned Writer may implements the TransformControl interface if it needs advanced control over the evaluation of the nested content. FreeMarker will call Writer.close() after the transform end-tag. Writer.close() must not close the Writer received as the out parameter (so if you are using a FilterWriter, you must override FilterWriter.close(), as by default that closes the wrapped Writer). Since 2.3.27 its also allowed to return the out writer as is, in which case it won't be closed.