You can create a class that implements NodeValueRenderer if you want to customize the elements of a NodeValueTemplate based on node data. When you declare the getValue() method, you can use NodeTemplateContext to retrieve current node data. See the following example implementation:
class SourcePictureRenderer implements NodeValueRenderer
{
@Override
public String getValue(NodeTemplateContext context)
{
String countryCode = (String) context.getNodeContext()
.getNode()
.getRecord()
.get(Path.parse("/country_code"));
if(StringUtils.isEmpty(countryCode))
{
return "";
}
switch (countryCode)
{
case "fr":
return "/common/icons/french_flag.png"
case "vn":
return "/common/icons/vietnam_flag.png"
case "us"
return "/common/icons/usa_flag.png"
default:
return "";
}
}
}
Once created, you can pass the class to certain template element methods:
NodeImage.setBindingSource(nodeValueRendererInstance)
NodeTextBlock.setBindingText(nodeValueRendererInstance)
The following shows how the example NodeValueRenderer above can be passed to a setBindingSource() method to retrieve an image:
NodeImage avatar = new NodeImage(); avatar.setBindingSource(new SourcePictureRenderer())