Search in sources :

Example 1 with SlingContext

use of org.apache.sling.scripting.thymeleaf.SlingContext in project sling by apache.

the class SlingEngineContextFactory method createEngineContext.

@Override
public IEngineContext createEngineContext(final IEngineConfiguration configuration, final TemplateData templateData, final Map<String, Object> templateResolutionAttributes, final IContext context) {
    if (context instanceof SlingContext) {
        // TODO web context
        final SlingContext slingContext = (SlingContext) context;
        final ResourceResolver resourceResolver = slingContext.getResourceResolver();
        final Locale locale = context.getLocale();
        final Set<String> variableNames = context.getVariableNames();
        final Map<String, Object> variables = new LinkedHashMap<>(variableNames.size() + 1, 1.0f);
        for (final String variableName : variableNames) {
            variables.put(variableName, context.getVariable(variableName));
        }
        return new SlingEngineContext(resourceResolver, configuration, templateData, templateResolutionAttributes, locale, variables);
    } else {
        throw new IllegalStateException("context is not an instance of SlingContext");
    }
}
Also used : Locale(java.util.Locale) SlingContext(org.apache.sling.scripting.thymeleaf.SlingContext) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with SlingContext

use of org.apache.sling.scripting.thymeleaf.SlingContext in project sling by apache.

the class SlingResourceTemplateResolver method resolveTemplate.

@Override
public TemplateResolution resolveTemplate(final IEngineConfiguration engineConfiguration, final IContext context, final String ownerTemplate, final String template, final Map<String, Object> templateResolutionAttributes) {
    logger.debug("resolving template '{}'", template);
    if (context instanceof SlingContext) {
        final SlingContext slingContext = (SlingContext) context;
        final ResourceResolver resourceResolver = slingContext.getResourceResolver();
        final Resource resource = resourceResolver.getResource(template);
        if (resource == null) {
            logger.warn("resource for template '{}' is null, not resolving template", template);
            return null;
        }
        final ITemplateResource templateResource = new SlingTemplateResource(resource);
        final boolean templateResourceExistenceVerified = false;
        final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource);
        if (templateMode == null) {
            logger.warn("template mode for template '{}' is null, not resolving template", template);
            return null;
        } else {
            logger.debug("using template mode {} for template '{}'", templateMode, template);
            final boolean useDecoupledLogic = templateMode.isMarkup() && configuration.useDecoupledLogic();
            final ICacheEntryValidity validity = NonCacheableCacheEntryValidity.INSTANCE;
            return new TemplateResolution(templateResource, templateResourceExistenceVerified, templateMode, useDecoupledLogic, validity);
        }
    } else {
        logger.error("context is not an instance of SlingContext");
        return null;
    }
}
Also used : TemplateMode(org.thymeleaf.templatemode.TemplateMode) SlingContext(org.apache.sling.scripting.thymeleaf.SlingContext) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource) Resource(org.apache.sling.api.resource.Resource) ICacheEntryValidity(org.thymeleaf.cache.ICacheEntryValidity)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SlingContext (org.apache.sling.scripting.thymeleaf.SlingContext)2 LinkedHashMap (java.util.LinkedHashMap)1 Locale (java.util.Locale)1 Resource (org.apache.sling.api.resource.Resource)1 ICacheEntryValidity (org.thymeleaf.cache.ICacheEntryValidity)1 TemplateMode (org.thymeleaf.templatemode.TemplateMode)1 TemplateResolution (org.thymeleaf.templateresolver.TemplateResolution)1 ITemplateResource (org.thymeleaf.templateresource.ITemplateResource)1