- 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.
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 -
Method Summary
-
Field Details
-
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 returnnull
in this case.Null vs. UNDEFINED
There is a subtle difference in how a
RenderSession
treatsnull
values versus how it treatsUNDEFINED
.null
is a valid and legitimate value for a template variable. If the value of a bean property or map key isnull
, the corresponding template variable will be set to whatevernull
is stringified to —. The default stringifier stringifiesnull
to an empty string. If, on the other hand, theRenderSession
receivesUNDEFINED
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
treatnull
just likeUNDEFINED
: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
Returns the value identified by the specified name from the specified source data object. If the source data object is aMap
,name
would likely be a map key; if it is a JavaBean,name
would likely be a bean property. However, it is up to individualAccessor
implementations to determine the type of objects they provide access to, and how names are to be interpreted.- Parameters:
data
- the data to be accessedname
- the name by which to retrieve the desired value from the data- Returns:
- the value
-