private void renderPage( final HttpServletRequest req, String reqPagePath, HttpServletResponse res, Site site, Page page, String[] requestContext) throws PebbleException, IOException { TemplateContext global = new TemplateContext(); global.setRequestContext(requestContext); for (String key : req.getParameterMap().keySet()) { global.put(key, req.getParameter(key)); } global.put("page", makePageWrapper(page)); populateSiteInfo(req, page, site, global); List<TemplateContext> components = new ArrayList<TemplateContext>(); for (Component component : page.getComponentsSet()) { TemplateContext local = new TemplateContext(); component.handle(page, local, global); components.add(local); } global.put("components", components); PebbleTemplate compiledTemplate = engine.getTemplate(site.getTheme().getType() + "/" + page.getTemplate().getFilePath()); res.setStatus(200); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter(), global); }
@Test public void testEscapeCharactersText() throws PebbleException, IOException { PebbleTemplate template = pebble.getTemplate("template.escapeCharactersInText.peb"); Map<String, Object> context = new HashMap<>(); Writer writer = new StringWriter(); template.evaluate(writer, context); }
@Test(expected = ParserException.class) public void testStringWithDifferentQuotationMarks() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate("{{'test\"}}"); Writer writer = new StringWriter(); template.evaluate(writer); assertEquals("test", writer.toString()); }
@Test public void testStringConstantWithLinebreak() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate("{{ 'test\ntest' }}"); Writer writer = new StringWriter(); template.evaluate(writer); assertEquals("test\ntest", writer.toString()); }
@Test public void testExpressionInArguments() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate("{{ input(1 + 1) }}{% macro input(value) %}{{value}}{% endmacro %}"); Writer writer = new StringWriter(); template.evaluate(writer); assertEquals("2", writer.toString()); }
@Test public void testSingleQuoteWithinDoubleQuotes() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate("{{\"te'st\"}}"); Writer writer = new StringWriter(); template.evaluate(writer); assertEquals("te'st", writer.toString()); template = pebble.getTemplate("{{\"te\\'st\"}}"); writer = new StringWriter(); template.evaluate(writer); assertEquals("te\\'st", writer.toString()); template = pebble.getTemplate("{{'te\\'st'}}"); writer = new StringWriter(); template.evaluate(writer); assertEquals("te'st", writer.toString()); }
@Test public void testAttributeNamePrefixedWithOperatorName() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate("{{ foo.org }}"); Map<String, Object> context = new HashMap<>(); context.put("foo", new Foo("success")); Writer writer = new StringWriter(); template.evaluate(writer, context); assertEquals("success", writer.toString()); }
@Test(expected = PebbleException.class) public void testIncorrectlyNamedArgument() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate( "{{ 'This is a test of the abbreviate filter' | abbreviate(WRONG=16) }}"); Writer writer = new StringWriter(); template.evaluate(writer); assertEquals("This is a tes...", writer.toString()); }
@Test public void testVariableNamePrefixedWithOperatorName() throws PebbleException, IOException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); PebbleTemplate template = pebble.getTemplate( "{{ organization }} {{ nothing }} {{ andy }} {{ equalsy }} {{ istanbul }}"); Map<String, Object> context = new HashMap<>(); context.put("organization", "organization"); context.put("nothing", "nothing"); context.put("andy", "andy"); context.put("equalsy", "equalsy"); context.put("istanbul", "istanbul"); Writer writer = new StringWriter(); template.evaluate(writer, context); assertEquals("organization nothing andy equalsy istanbul", writer.toString()); }
@Test(expected = PebbleException.class) public void testPositionalArgumentAfterNamedArguments() throws PebbleException, IOException, ParseException { Loader loader = new StringLoader(); PebbleEngine pebble = new PebbleEngine(loader); String source = "{{ stringDate | date(existingFormat='yyyy-MMMM-d', 'yyyy/MMMM/d') }}"; PebbleTemplate template = pebble.getTemplate(source); Map<String, Object> context = new HashMap<>(); DateFormat format = new SimpleDateFormat("yyyy-MMMM-d"); Date realDate = format.parse("2012-July-01"); context.put("stringDate", format.format(realDate)); Writer writer = new StringWriter(); template.evaluate(writer, context); assertEquals("2012/July/1", writer.toString()); }
private void errorPage( final HttpServletRequest req, HttpServletResponse res, Site site, int errorCode) throws ServletException, IOException { CMSTheme cmsTheme = site.getTheme(); if (cmsTheme != null && cmsTheme.definesPath(errorCode + ".html")) { try { PebbleTemplate compiledTemplate = engine.getTemplate(cmsTheme.getType() + "/" + errorCode + ".html"); TemplateContext global = new TemplateContext(); populateSiteInfo(req, null, site, global); res.setStatus(errorCode); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter(), global); } catch (PebbleException e) { throw new ServletException("Could not render error page for " + errorCode); } } else { res.sendError(errorCode, req.getRequestURI()); } }
private void handleLeadingSlash(final HttpServletRequest req, HttpServletResponse res, Site sites) throws PebbleException, IOException, ServletException { if (req.getMethod().equals("GET")) { res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("Location", rewritePageUrl(req)); return; } else if (req.getMethod().equals("POST")) { if (CoreConfiguration.getConfiguration().developmentMode()) { PebbleEngine engine = new PebbleEngine(new StringLoader()); engine.addExtension(new CMSExtensions()); PebbleTemplate compiledTemplate = engine.getTemplate( "<html><head></head><body><h1>POST action with backslash</h1><b>You posting data with a URL with a backslash. Alter the form to post with the same URL without the backslash</body></html>"); res.setStatus(500); res.setContentType("text/html"); compiledTemplate.evaluate(res.getWriter()); } else { errorPage(req, res, sites, 500); } } }