use of org.thymeleaf.engine.TemplateManager in project thymeleaf by thymeleaf.
the class StandardConditionalCommentProcessor method doProcess.
@Override
protected void doProcess(final ITemplateContext context, final IComment comment, final ICommentStructureHandler structureHandler) {
final StandardConditionalCommentUtils.ConditionalCommentParsingResult parsingResult = StandardConditionalCommentUtils.parseConditionalComment(comment);
if (parsingResult == null) {
// This is NOT a Conditional Comment. Just return
return;
}
final String commentStr = comment.getComment();
/*
* Next, we need to get the content of the Conditional Comment and process it as a piece of markup. In fact,
* we must process it as a template itself (a template fragment) so that all thymeleaf attributes and
* structures inside this content execute correctly, including references to context variables.
*/
final TemplateManager templateManager = context.getConfiguration().getTemplateManager();
final String parsableContent = commentStr.substring(parsingResult.getContentOffset(), parsingResult.getContentOffset() + parsingResult.getContentLen());
final TemplateModel templateModel = templateManager.parseString(context.getTemplateData(), parsableContent, comment.getLine(), comment.getCol(), // No need to force template mode
null, true);
final FastStringWriter writer = new FastStringWriter(200);
/*
* Rebuild the conditional comment start expression
*/
writer.write("[");
writer.write(commentStr, parsingResult.getStartExpressionOffset(), parsingResult.getStartExpressionLen());
writer.write("]>");
/*
* Process the parsable content
*/
templateManager.process(templateModel, context, writer);
/*
* Rebuild the conditional comment end expression
*/
writer.write("<![");
writer.write(commentStr, parsingResult.getEndExpressionOffset(), parsingResult.getEndExpressionLen());
writer.write("]");
/*
* Re-set the comment content, once processed
*/
structureHandler.setContent(writer.toString());
}
Aggregations