use of org.craftercms.core.util.template.CompiledTemplate in project core by craftercms.
the class TemplateProcessor method process.
/**
* Processes the content of certain nodes (found by the {@code NodeScanner} in the item's descriptor as templates,
* by compiling the node text templates through the {@code templateCompiler} and then processing the compiled
* template with a model returned by {@code modelFactory}.
*
* @throws ItemProcessingException if an error occurred while processing a template
*/
public Item process(Context context, CachingOptions cachingOptions, Item item) throws ItemProcessingException {
String descriptorUrl = item.getDescriptorUrl();
Document descriptorDom = item.getDescriptorDom();
if (descriptorDom != null) {
List<Node> templateNodes = templateNodeScanner.scan(descriptorDom);
if (CollectionUtils.isNotEmpty(templateNodes)) {
for (Node templateNode : templateNodes) {
String templateNodePath = templateNode.getUniquePath();
if (logger.isDebugEnabled()) {
logger.debug("Template found in " + descriptorUrl + " at " + templateNodePath);
}
String templateId = templateNodePath + "@" + descriptorUrl;
String template = templateNode.getText();
IdentifiableStringTemplateSource templateSource = new IdentifiableStringTemplateSource(templateId, template);
Object model = modelFactory.getModel(item, templateNode, template);
StringWriter output = new StringWriter();
try {
CompiledTemplate compiledTemplate = templateCompiler.compile(templateSource);
compiledTemplate.process(model, output);
} catch (TemplateException e) {
throw new ItemProcessingException("Unable to process the template " + templateId, e);
}
templateNode.setText(output.toString());
}
}
}
return item;
}
Aggregations