Java Code Examples for com.hubspot.jinjava.Jinjava#setResourceLocator()

The following examples show how to use com.hubspot.jinjava.Jinjava#setResourceLocator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtendsTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  locator = new ExtendsTagTestResourceLocator();

  JinjavaConfig conf = new JinjavaConfig();
  jinjava = new Jinjava(conf);
  jinjava.setResourceLocator(locator);
}
 
Example 2
Source File: FromTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  Jinjava jinjava = new Jinjava();
  jinjava.setResourceLocator(
    new ResourceLocator() {
      private RelativePathResolver relativePathResolver = new RelativePathResolver();

      @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
        );
      }

      @Override
      public Optional<LocationResolver> getLocationResolver() {
        return Optional.of(relativePathResolver);
      }
    }
  );

  interpreter = jinjava.newInterpreter();
  JinjavaInterpreter.pushCurrent(interpreter);

  context = interpreter.getContext();
  context.put("padding", 42);
}
 
Example 3
Source File: IncludeTagTest.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Test
public void itIncludesFileViaRelativePath() throws IOException {
  jinjava = new Jinjava();
  jinjava.setResourceLocator(
    new ResourceLocator() {
      private RelativePathResolver relativePathResolver = new RelativePathResolver();

      @Override
      public String getString(
        String fullName,
        Charset encoding,
        JinjavaInterpreter interpreter
      )
        throws IOException {
        return Resources.toString(
          Resources.getResource(String.format("%s", fullName)),
          StandardCharsets.UTF_8
        );
      }

      @Override
      public Optional<LocationResolver> getLocationResolver() {
        return Optional.of(relativePathResolver);
      }
    }
  );

  jinjava
    .getGlobalContext()
    .put(CURRENT_PATH_CONTEXT_KEY, "tags/includetag/includes-relative-path.jinja");
  RenderResult result = jinjava.renderForResult(
    Resources.toString(
      Resources.getResource("tags/includetag/includes-relative-path.jinja"),
      StandardCharsets.UTF_8
    ),
    new HashMap<>()
  );

  assertThat(result.getOutput().trim()).isEqualTo("INCLUDED");
}
 
Example 4
Source File: JinjavaEngine.java    From spark-template-engines with Apache License 2.0 4 votes vote down vote up
public JinjavaEngine(JinjavaConfig jinjavaConfig, ResourceLocator resourceLocator) {
    jinjava = new Jinjava(jinjavaConfig);
    jinjava.setResourceLocator(resourceLocator);
    context = jinjava.getGlobalContext();
}