Search in sources :

Example 6 with TemplateManager

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());
}
Also used : FastStringWriter(org.thymeleaf.util.FastStringWriter) TemplateManager(org.thymeleaf.engine.TemplateManager) StandardConditionalCommentUtils(org.thymeleaf.standard.util.StandardConditionalCommentUtils) TemplateModel(org.thymeleaf.engine.TemplateModel)

Aggregations

TemplateManager (org.thymeleaf.engine.TemplateManager)6 TemplateModel (org.thymeleaf.engine.TemplateModel)4 FastStringWriter (org.thymeleaf.util.FastStringWriter)4 Writer (java.io.Writer)3 BigDecimal (java.math.BigDecimal)2 TemplateEngineException (org.thymeleaf.exceptions.TemplateEngineException)2 TemplateOutputException (org.thymeleaf.exceptions.TemplateOutputException)2 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)2 IOException (java.io.IOException)1 StandardConditionalCommentUtils (org.thymeleaf.standard.util.StandardConditionalCommentUtils)1 LazyProcessingCharSequence (org.thymeleaf.util.LazyProcessingCharSequence)1