Interface Accessor<T>

Type Parameters:
T - the type of the source data object
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Accessor<T>
Accessors are used to extract values from objects. The RenderSession uses them to extract values from the objects passed to insert() and populate(). Object access is name-based and requires a mapping between template variables and bean properties or map keys. By default, Klojang Templates assumes an as-is mapping between the two, but you can use name mappers for more sophisticated mappings.
Author:
Ayco Holleman
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Object
    The value that should be returned by accessors if a template variable cannot be mapped to a value in the source data object.
  • Method Summary

    Modifier and Type
    Method
    Description
    access(T data, String name)
    Returns the value identified by the specified name from the specified source data object.
  • Field Details

    • UNDEFINED

      static final Object UNDEFINED

      The value that should be returned by accessors if a template variable cannot be mapped to a value in the source data object. Accessor implementations should not throw an exception and they should not return null in this case.

      Null vs. UNDEFINED

      There is a subtle difference in how a RenderSession treats null values versus how it treats UNDEFINED. null is a valid and legitimate value for a template variable. If the value of a bean property or map key is null, the corresponding template variable will be set to whatever null is stringified to —. The default stringifier stringifies null to an empty string. If, on the other hand, the RenderSession receives UNDEFINED as the value for a template variable, it will just skip setting that variable. By itself this will make no difference when the template is rendered. An unset variable will be replaced with "nothing" — i.e. an empty string. However, it does make a difference if you want to set all unset variables to some default value after you have populated your template with model objects, hash maps, and/or anything else for which you have defined an accessor:

      
       CompanyDao dao = new CompanyDao();
       Template template = Template.fromResource(getClass(), "/views/companies.html");
       RenderSession session = template.newRenderSession();
       session.populate("companies", dao.list());
       session.getAllUnsetVariables().forEach(var -> session.setPath(var, i -> "(unknown)");
       

      You can make the RenderSession treat null just like UNDEFINED:

      
       AccessorRegistry accessors = AccessorRegistry.build()
          .nullEqualsUndefined(true);
          .freeze();
       Template template = Template.fromResource(getClass(), "/views/companies.html");
       RenderSession session = template.newRenderSession(accessors);
       
      See Also:
  • Method Details

    • access

      Object access(T data, String name)
      Returns the value identified by the specified name from the specified source data object. If the source data object is a Map, name would likely be a map key; if it is a JavaBean, name would likely be a bean property. However, it is up to individual Accessor implementations to determine the type of objects they provide access to, and how names are to be interpreted.
      Parameters:
      data - the data to be accessed
      name - the name by which to retrieve the desired value from the data
      Returns:
      the value