/** * Creates a new interpreter instance using the global context and global config * * @return a new interpreter instance */ public JinjavaInterpreter newInterpreter() { return new JinjavaInterpreter(this, this.getGlobalContext(), this.getGlobalConfig()); }
@Test public void itAvoidsNestedImportCycle() throws IOException { Jinjava jinjava = new Jinjava(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); interpreter.render(Resources.toString(Resources.getResource("tags/importtag/a-imports-b.jinja"), StandardCharsets.UTF_8)); assertThat(context.get("a")).isEqualTo("foo"); assertThat(context.get("b")).isEqualTo("bar"); assertThat(interpreter.getErrorsCopy().get(0).getMessage()).contains("Rendering cycle detected:", "b-imports-a.jinja"); }
@Test public void itAvoidsNestedImportCycle() throws IOException { Jinjava jinjava = new Jinjava(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); interpreter.render(Resources.toString(Resources.getResource("tags/importtag/a-imports-b.jinja"), StandardCharsets.UTF_8)); assertThat(context.get("a")).isEqualTo("foo"); assertThat(context.get("b")).isEqualTo("bar"); assertThat(interpreter.getErrorsCopy().get(0).getMessage()).contains("Rendering cycle detected:", "b-imports-a.jinja"); }
@Test public void itAvoidsSimpleImportCycle() throws IOException { Jinjava jinjava = new Jinjava(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); interpreter.render(Resources.toString(Resources.getResource("tags/importtag/imports-self.jinja"), StandardCharsets.UTF_8)); assertThat(context.get("c")).isEqualTo("hello"); assertThat(interpreter.getErrorsCopy().get(0).getMessage()).contains("Rendering cycle detected:", "imports-self.jinja"); }
@Test public void itAvoidsSimpleImportCycle() throws IOException { Jinjava jinjava = new Jinjava(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); interpreter.render(Resources.toString(Resources.getResource("tags/importtag/imports-self.jinja"), StandardCharsets.UTF_8)); assertThat(context.get("c")).isEqualTo("hello"); assertThat(interpreter.getErrorsCopy().get(0).getMessage()).contains("Rendering cycle detected:", "imports-self.jinja"); }
interpreter.render(node); } else { JinjavaInterpreter child = new JinjavaInterpreter(interpreter); child.render(node);
Node node = interpreter.parse(template); JinjavaInterpreter child = new JinjavaInterpreter(interpreter); child.render(node);
@Before public void setup() { Jinjava jinjava = new Jinjava(); jinjava.setResourceLocator(new ResourceLocator() { @Override public String getString(String fullName, Charset encoding, JinjavaInterpreter interpreter) throws IOException { return Resources.toString( Resources.getResource(String.format("tags/macrotag/%s", fullName)), StandardCharsets.UTF_8); } }); context = new Context(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); JinjavaInterpreter.pushCurrent(interpreter); context.put("padding", 42); }
@Before public void setup() { Jinjava jinjava = new Jinjava(); jinjava.setResourceLocator(new ResourceLocator() { @Override public String getString(String fullName, Charset encoding, JinjavaInterpreter interpreter) throws IOException { return Resources.toString( Resources.getResource(String.format("tags/macrotag/%s", fullName)), StandardCharsets.UTF_8); } }); context = new Context(); interpreter = new JinjavaInterpreter(jinjava, context, jinjava.getGlobalConfig()); JinjavaInterpreter.pushCurrent(interpreter); context.put("padding", 42); }
@Override public String interpret(TagNode tagNode, JinjavaInterpreter interpreter) { HelperStringTokenizer helper = new HelperStringTokenizer(tagNode.getHelpers()); if (!helper.hasNext()) { throw new TemplateSyntaxException(tagNode.getMaster().getImage(), "Tag 'include' expects template path", tagNode.getLineNumber(), tagNode.getStartPosition()); } String path = StringUtils.trimToEmpty(helper.next()); String templateFile = interpreter.resolveString(path, tagNode.getLineNumber(), tagNode.getStartPosition()); try { interpreter.getContext().getIncludePathStack().push(templateFile, tagNode.getLineNumber(), tagNode.getStartPosition()); } catch (IncludeTagCycleException e) { interpreter.addError(new TemplateError(ErrorType.WARNING, ErrorReason.EXCEPTION, ErrorItem.TAG, "Include cycle detected for path: '" + templateFile + "'", null, tagNode.getLineNumber(), tagNode.getStartPosition(), e, BasicTemplateErrorCategory.INCLUDE_CYCLE_DETECTED, ImmutableMap.of("path", templateFile))); return ""; } try { String template = interpreter.getResource(templateFile); Node node = interpreter.parse(template); interpreter.getContext().addDependency("coded_files", templateFile); JinjavaInterpreter child = new JinjavaInterpreter(interpreter); String result = child.render(node); interpreter.addAllErrors(child.getErrorsCopy()); return result; } catch (IOException e) { throw new InterpretException(e.getMessage(), e, tagNode.getLineNumber(), tagNode.getStartPosition()); } finally { interpreter.getContext().getIncludePathStack().pop(); } }
@Before public void setup() { validationFilter = new ValidationFilter(); ELFunctionDefinition validationFunction = new ELFunctionDefinition("", "validation_test", ValidationModeTest.class, "validationTestFunction"); jinjava = new Jinjava(); jinjava.getGlobalContext().registerFilter(validationFilter); jinjava.getGlobalContext().registerFunction(validationFunction); interpreter = jinjava.newInterpreter(); context = interpreter.getContext(); validatingInterpreter = new JinjavaInterpreter(jinjava, context, JinjavaConfig.newBuilder().withValidationMode(true).build()); JinjavaInterpreter.pushCurrent(interpreter); }
@Before public void setup() { validationFilter = new ValidationFilter(); ELFunctionDefinition validationFunction = new ELFunctionDefinition("", "validation_test", ValidationModeTest.class, "validationTestFunction"); jinjava = new Jinjava(); jinjava.getGlobalContext().registerFilter(validationFilter); jinjava.getGlobalContext().registerFunction(validationFunction); interpreter = jinjava.newInterpreter(); context = interpreter.getContext(); validatingInterpreter = new JinjavaInterpreter(jinjava, context, JinjavaConfig.newBuilder().withValidationMode(true).build()); JinjavaInterpreter.pushCurrent(interpreter); }
JinjavaInterpreter interpreter = new JinjavaInterpreter(this, context, renderConfig); JinjavaInterpreter.pushCurrent(interpreter);