public JtwigContext() { this.functionRepository = new DefaultFunctionRepository(); this.modelMap = new JtwigModelMap(); }
public JtwigContext() { this.functionRepository = new DefaultFunctionRepository(); this.modelMap = new JtwigModelMap(); }
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { // Adding model information JtwigModelMap modelMap = new JtwigModelMap() .add(model) .add("beans", new BeanResolver(getApplicationContext())) .add("theme", getThemeName(request)) .add("request", request); Object token = request.getAttribute("org.springframework.security.web.csrf.CsrfToken"); if(token != null){ modelMap.add("csrf", token); }else{ modelMap.add("csrf", Undefined.UNDEFINED); } if (log.isDebugEnabled()) { log.debug("Rendering Jtwig templates [" + getUrl() + "] in JtwigView '" + getBeanName() + "'"); log.debug("Model: " + modelMap); } if (this.getEncoding() != null) { response.setCharacterEncoding(this.getEncoding()); } getContent().render(RenderContext.create(getConfiguration().render(), modelMap, getViewResolver().functionResolver(), response.getOutputStream())); response.getOutputStream().flush(); response.getOutputStream().close(); }
@Override public String renderAsString(Path templatePath, Map<String, Object> data) throws TemplateRenderingException { JtwigTemplate template = new JtwigTemplate(new JailedPathJtwigResource(fileManager.resolvePermanentFilePath(Paths.get(".")), templatePath), new JtwigConfiguration()); JtwigModelMap model = new JtwigModelMap(); model.add(data); try { return template.output(model); } catch (CompileException | RenderException | ParseException e) { throw new TemplateRenderingException(e); } } }
@Override public void render(Path templatePath, Map<String, Object> data, OutputStream outputStream) throws TemplateRenderingException { JtwigTemplate template = new JtwigTemplate(new JailedPathJtwigResource(fileManager.resolvePermanentFilePath(Paths.get(".")), templatePath), new JtwigConfiguration()); JtwigModelMap model = new JtwigModelMap(); model.add(data); try { template.output(outputStream, model); } catch (CompileException | RenderException | ParseException e) { throw new TemplateRenderingException(e); } }