Search in sources :

Example 1 with TemplateMode

use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.

the class PatternTemplateModeProviderIT method provideTemplateMode_XML.

@Test
public void provideTemplateMode_XML() throws Exception {
    final Resource resource = mockResource("/apps/thymeleaf/page/foo.xml");
    final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
    assertThat(templateMode, is(TemplateMode.XML));
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 2 with TemplateMode

use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.

the class PatternTemplateModeProviderIT method provideTemplateMode_HTML.

@Test
public void provideTemplateMode_HTML() throws Exception {
    final Resource resource = mockResource("/apps/thymeleaf/page/foo.html");
    final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
    assertThat(templateMode, is(TemplateMode.HTML));
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 3 with TemplateMode

use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.

the class PatternTemplateModeProviderIT method provideTemplateMode_TEXT.

@Test
public void provideTemplateMode_TEXT() throws Exception {
    final Resource resource = mockResource("/apps/thymeleaf/text/foo.txt");
    final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
    assertThat(templateMode, is(TemplateMode.TEXT));
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Example 4 with TemplateMode

use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.

the class TemplateManager method parseAndProcess.

/*
     * -------------------------
     * PARSE-AND-PROCESS methods
     * -------------------------
     *
     * These methods perform the whole cycle of a template's processing: resolving, parsing and processing.
     * This is only meant to be called from the TemplateEngine
     */
public void parseAndProcess(final TemplateSpec templateSpec, final IContext context, final Writer writer) {
    Validate.notNull(templateSpec, "Template Specification cannot be null");
    Validate.notNull(context, "Context cannot be null");
    Validate.notNull(writer, "Writer cannot be null");
    // TemplateSpec will already have validated its contents, so need to do it here (template selectors,
    // resolution attributes, etc.)
    final String template = templateSpec.getTemplate();
    final Set<String> templateSelectors = templateSpec.getTemplateSelectors();
    final TemplateMode templateMode = templateSpec.getTemplateMode();
    final Map<String, Object> templateResolutionAttributes = templateSpec.getTemplateResolutionAttributes();
    final TemplateCacheKey cacheKey = new TemplateCacheKey(// ownerTemplate
    null, template, templateSelectors, // lineOffset, colOffset
    0, // lineOffset, colOffset
    0, templateMode, templateResolutionAttributes);
    /*
         * First look at the cache - it might be already cached
         */
    if (this.templateCache != null) {
        final TemplateModel cached = this.templateCache.get(cacheKey);
        if (cached != null) {
            final IEngineContext engineContext = EngineContextManager.prepareEngineContext(this.configuration, cached.getTemplateData(), templateResolutionAttributes, context);
            /*
                 * Create the handler chain to process the data.
                 * This is PARSE + PROCESS, so its called from the TemplateEngine, and the only case in which we should apply
                 * both pre-processors and post-processors (besides creating a last output-to-writer step)
                 */
            final ProcessorTemplateHandler processorTemplateHandler = new ProcessorTemplateHandler();
            final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, writer);
            cached.process(processingHandlerChain);
            EngineContextManager.disposeEngineContext(engineContext);
            return;
        }
    }
    /*
         * Resolve the template
         */
    final TemplateResolution templateResolution = resolveTemplate(this.configuration, context, null, template, templateResolutionAttributes, true);
    /*
         * Build the TemplateData object
         */
    final TemplateData templateData = buildTemplateData(templateResolution, template, templateSelectors, templateMode, true);
    /*
         * Prepare the context instance that corresponds to this execution of the template engine
         */
    final IEngineContext engineContext = EngineContextManager.prepareEngineContext(this.configuration, templateData, templateResolutionAttributes, context);
    /*
         * Create the handler chain to process the data.
         * This is PARSE + PROCESS, so its called from the TemplateEngine, and the only case in which we should apply
         * both pre-processors and post-processors (besides creating a last output-to-writer step)
         */
    final ProcessorTemplateHandler processorTemplateHandler = new ProcessorTemplateHandler();
    final ITemplateHandler processingHandlerChain = createTemplateProcessingHandlerChain(engineContext, true, true, processorTemplateHandler, writer);
    /*
         * Obtain the parser
         */
    final ITemplateParser parser = getParserForTemplateMode(engineContext.getTemplateMode());
    /*
         * If the resolved template is cacheable, so we will first read it as an object, cache it, and then process it
         */
    if (templateResolution.getValidity().isCacheable() && this.templateCache != null) {
        // Create the handler chain to create the Template object
        final ModelBuilderTemplateHandler builderHandler = new ModelBuilderTemplateHandler(this.configuration, templateData);
        // Process the template into a TemplateModel
        parser.parseStandalone(this.configuration, null, template, templateSelectors, templateData.getTemplateResource(), engineContext.getTemplateMode(), templateResolution.getUseDecoupledLogic(), builderHandler);
        // Obtain the TemplateModel
        final TemplateModel templateModel = builderHandler.getModel();
        // Put the new template into cache
        this.templateCache.put(cacheKey, templateModel);
        // Process the read (+cached) template itself
        templateModel.process(processingHandlerChain);
    } else {
        //  Process the template, which is not cacheable (so no worry about caching)
        parser.parseStandalone(this.configuration, null, template, templateSelectors, templateData.getTemplateResource(), engineContext.getTemplateMode(), templateResolution.getUseDecoupledLogic(), processingHandlerChain);
    }
    /*
         * Dispose the engine context now that processing has been done
         */
    EngineContextManager.disposeEngineContext(engineContext);
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) TemplateCacheKey(org.thymeleaf.cache.TemplateCacheKey) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateParser(org.thymeleaf.templateparser.ITemplateParser) IEngineContext(org.thymeleaf.context.IEngineContext)

Example 5 with TemplateMode

use of org.thymeleaf.templatemode.TemplateMode in project sling by apache.

the class TemplateManager method buildTemplateData.

private static TemplateData buildTemplateData(final TemplateResolution templateResolution, final String template, final Set<String> templateSelectors, final TemplateMode templateMode, final boolean useCache) {
    final TemplateMode definitiveTemplateMode = (templateMode == null ? templateResolution.getTemplateMode() : templateMode);
    final ICacheEntryValidity definitiveCacheEntryValidity = (useCache ? templateResolution.getValidity() : NonCacheableCacheEntryValidity.INSTANCE);
    return new TemplateData(template, templateSelectors, templateResolution.getTemplateResource(), definitiveTemplateMode, definitiveCacheEntryValidity);
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Aggregations

TemplateMode (org.thymeleaf.templatemode.TemplateMode)11 Resource (org.apache.sling.api.resource.Resource)7 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)6 Test (org.junit.Test)6 ICacheEntryValidity (org.thymeleaf.cache.ICacheEntryValidity)3 TemplateCacheKey (org.thymeleaf.cache.TemplateCacheKey)3 ITemplateParser (org.thymeleaf.templateparser.ITemplateParser)3 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)3 IEngineContext (org.thymeleaf.context.IEngineContext)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 SlingContext (org.apache.sling.scripting.thymeleaf.SlingContext)1 ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)1