/** * Initializes the evaluation context with settings from the engine. * * @param locale The desired locale * @return The evaluation context */ private EvaluationContextImpl initContext(Locale locale) { locale = locale == null ? this.engine.getDefaultLocale() : locale; // globals ScopeChain scopeChain = new ScopeChain(); Map<String, Object> globals = new HashMap<>(); globals.put("locale", locale); globals.put("template", this); globals.put("_context", new GlobalContext(scopeChain)); scopeChain.pushScope(globals); // global vars provided from extensions scopeChain.pushScope(this.engine.getExtensionRegistry().getGlobalVariables()); return new EvaluationContextImpl(this, this.engine.isStrictVariables(), locale, this.engine.getExtensionRegistry(), this.engine.getTagCache(), this.engine.getExecutorService(), new ArrayList<>(), new HashMap<>(), scopeChain, null, this.engine.getEvaluationOptions()); }
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = getLanguageOrDefault(language); } Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = getLocaleOrDefault(language); } try { PebbleEngine stringEngine = new PebbleEngine.Builder() .loader(new StringLoader()) .strictVariables(engine.isStrictVariables()) .templateCache(null) .build(); PebbleTemplate template = stringEngine.getTemplate(templateContent); template.evaluate(writer, model, locale); writer.flush(); } catch (Exception e) { throw new PippoRuntimeException(e); } }
/** * Initializes the evaluation context with settings from the engine. * * @param locale The desired locale * @return The evaluation context */ private EvaluationContext initContext(Locale locale) { locale = locale == null ? this.engine.getDefaultLocale() : locale; // globals Map<String, Object> globals = new HashMap<>(); globals.put("locale", locale); globals.put("template", this); ScopeChain scopeChain = new ScopeChain(globals); // global vars provided from extensions scopeChain.pushScope(this.engine.getExtensionRegistry().getGlobalVariables()); return new EvaluationContext(this, this.engine.isStrictVariables(), locale, this.engine.getExtensionRegistry(), this.engine.getTagCache(), this.engine.getExecutorService(), new ArrayList<PebbleTemplateImpl>(), scopeChain, null); }
/** * Initializes the evaluation context with settings from the engine. * * @param locale The desired locale * @return The evaluation context */ private EvaluationContextImpl initContext(Locale locale) { locale = locale == null ? this.engine.getDefaultLocale() : locale; // globals ScopeChain scopeChain = new ScopeChain(); Map<String, Object> globals = new HashMap<>(); globals.put("locale", locale); globals.put("template", this); globals.put("_context", new GlobalContext(scopeChain)); scopeChain.pushScope(globals); // global vars provided from extensions scopeChain.pushScope(this.engine.getExtensionRegistry().getGlobalVariables()); return new EvaluationContextImpl(this, this.engine.isStrictVariables(), locale, this.engine.getExtensionRegistry(), this.engine.getTagCache(), this.engine.getExecutorService(), new ArrayList<>(), new HashMap<>(), scopeChain, null, this.engine.getEvaluationOptions()); }