add ModelTemplateWriter for string templated Model serialization
alexvigdor opened this issue · 0 comments
Groovity currently provides specialized ModelWalkers to generate JSON and XML from Models (ModelJsonWriter and ModelXmlWriter). It might also make sense to provide a ModelTemplateWriter that could be used to generate string-templated output, e.g. HTML or Markdown, from a Model.
One possible design approach would be to exclusively serialize templates and rely on ModelFilter.transforms to insert templates at the appropriate places while walking a logical Model.
It would make sense to be able to leverage this solution using the write
tag the way json and xml are accessible formats.
This might be a more flexible approach to templating for models with mixed collections of types or composed of traits for which ModelFilter transforms can be independently specified and composed together as needed.
To express the concept in pseudo-code, something like this
model = [new Story(...), new Video(...), new Video(...)]
def filters = [
ModelFilter.transform(Story.class){m-> <~<li>Story ${m.title} - ${m.wordCount} words</li>~>},
ModelFilter.transform(Video.class){m-> <~<li>Video ${m.title} - ${m.duration} seconds</li>~>}
]
<~
<html>
<ul>
<g:write value"${model}" filter="${filters}" format="~" />
</ul>
</html>
~>