Java Code Examples for com.hubspot.jinjava.interpret.JinjavaInterpreter#getCurrent()

The following examples show how to use com.hubspot.jinjava.interpret.JinjavaInterpreter#getCurrent() . 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: Functions.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@JinjavaDoc(
  value = "Only usable within blocks, will render the contents of the parent block by calling super.",
  snippets = {
    @JinjavaSnippet(
      desc = "This gives back the results of the parent block",
      code = "{% block sidebar %}\n" +
      "    <h3>Table Of Contents</h3>\n\n" +
      "    ...\n" +
      "    {{ super() }}\n" +
      "{% endblock %}"
    )
  }
)
public static String renderSuperBlock() {
  JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();
  LengthLimitingStringBuilder result = new LengthLimitingStringBuilder(
    interpreter.getConfig().getMaxOutputSize()
  );

  List<? extends Node> superBlock = interpreter.getContext().getSuperBlock();
  if (superBlock != null) {
    for (Node n : superBlock) {
      result.append(n.render(interpreter));
    }
  }

  return result.toString();
}
 
Example 2
Source File: DeferredValueUtils.java    From jinjava with Apache License 2.0 5 votes vote down vote up
private static Set<DeferredTag> getDeferredTags(List<Node> nodes, int depth) {
  // precaution - templates are parsed with this render depth so in theory the depth should never be exceeded
  Set<DeferredTag> deferredTags = new HashSet<>();
  int maxRenderDepth = JinjavaInterpreter.getCurrent() == null
    ? 3
    : JinjavaInterpreter.getCurrent().getConfig().getMaxRenderDepth();
  if (depth > maxRenderDepth) {
    return deferredTags;
  }
  for (Node node : nodes) {
    getDeferredTags(node).ifPresent(deferredTags::addAll);
    deferredTags.addAll(getDeferredTags(node.getChildren(), depth + 1));
  }
  return deferredTags;
}
 
Example 3
Source File: MacroFunction.java    From jinjava with Apache License 2.0 4 votes vote down vote up
@Override
public Object doEvaluate(
  Map<String, Object> argMap,
  Map<String, Object> kwargMap,
  List<Object> varArgs
) {
  JinjavaInterpreter interpreter = JinjavaInterpreter.getCurrent();
  Optional<String> importFile = Optional.ofNullable(
    (String) localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY)
  );

  // pushWithoutCycleCheck() is used to here so that macros calling macros from the same file will not throw a TagCycleException
  importFile.ifPresent(
    path ->
      interpreter
        .getContext()
        .getCurrentPathStack()
        .pushWithoutCycleCheck(
          path,
          interpreter.getLineNumber(),
          interpreter.getPosition()
        )
  );

  try (InterpreterScopeClosable c = interpreter.enterScope()) {
    interpreter.setLineNumber(definitionLineNumber);
    interpreter.setPosition(definitionStartPosition);

    for (Map.Entry<String, Object> scopeEntry : localContextScope
      .getScope()
      .entrySet()) {
      if (scopeEntry.getValue() instanceof MacroFunction) {
        interpreter.getContext().addGlobalMacro((MacroFunction) scopeEntry.getValue());
      } else {
        interpreter.getContext().put(scopeEntry.getKey(), scopeEntry.getValue());
      }
    }

    // named parameters
    for (Map.Entry<String, Object> argEntry : argMap.entrySet()) {
      interpreter.getContext().put(argEntry.getKey(), argEntry.getValue());
    }
    // parameter map
    interpreter.getContext().put("kwargs", kwargMap);
    // varargs list
    interpreter.getContext().put("varargs", varArgs);

    LengthLimitingStringBuilder result = new LengthLimitingStringBuilder(
      interpreter.getConfig().getMaxOutputSize()
    );

    for (Node node : content) {
      result.append(node.render(interpreter));
    }

    if (!interpreter.getContext().getDeferredNodes().isEmpty()) {
      throw new DeferredValueException(
        getName(),
        interpreter.getLineNumber(),
        interpreter.getPosition()
      );
    }

    return result.toString();
  } finally {
    importFile.ifPresent(path -> interpreter.getContext().getCurrentPathStack().pop());
  }
}