samedi 16 mai 2015

Getting big objects with FreeMarker

I think it would be really great, if template designer would be able to access all the fields he wants from the given object. So in java instead of:

Map<String, Object> fields = new HashMap<>();
fields.add("x", unit.getX());
fields.add("y", unit.getY());
fields.add("z", unit.getZ());
Map<String, Object> dataModel = new HashMap<>();
dataModel.put("unit", fields);

I could make:

Map<String, Object> dataModel = new HashMap<>();
dataModel.put("unit", unit);

and ftl designer would have possibility to access x, y and z of the unit somehow.

So I have got 2 questions regarding that: 1. How to make good access to the fields of Unit object in ftl file? 2. How can ftl designer know, what are the possible fields that he can use?

So far I have made point 1 to work with reflection. Unit class is implementing TemplateHashModel and in get(String) I made it to find field by name and then parse the value to SimpleScalar, SimpleNumber or other TemplateHashModel. Is it a good solution?





Aucun commentaire:

Enregistrer un commentaire