use of com.squarespace.template.Instruction in project template-compiler by Squarespace.
the class CoreFormattersTest method testApplyPartialRecursion.
@Test
public void testApplyPartialRecursion() throws CodeException {
String template = "{@|apply one}";
String partials = "{\"one\": \"1{@|apply two}\", \"two\": \"2{@|apply one}\"}";
String input = "{}";
Instruction inst = compiler().compile(template).code();
Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).maxPartialDepth(6).safeExecution(true).execute();
assertContext(ctx, "121212");
assertEquals(ctx.getErrors().size(), 1);
assertEquals(ctx.getErrors().get(0).getType(), APPLY_PARTIAL_RECURSION_DEPTH);
}
use of com.squarespace.template.Instruction in project template-compiler by Squarespace.
the class CoreFormattersTest method testApplyPartialRecursionDepth.
@Test
public void testApplyPartialRecursionDepth() throws CodeException {
String template = "{@|apply one}";
String partials = "{\"one\": \"1{@|apply two}\", \"two\": \"2{@|apply three}\", " + "\"three\": \"3{@|apply four}\", \"four\": \"4\"}";
String input = "{}";
Instruction inst = compiler().compile(template).code();
Context ctx = compiler().newExecutor().json(input).code(inst).partialsMap(partials).safeExecution(true).maxPartialDepth(3).execute();
assertContext(ctx, "123");
assertEquals(ctx.getErrors().size(), 1);
assertEquals(ctx.getErrors().get(0).getType(), APPLY_PARTIAL_RECURSION_DEPTH);
}
use of com.squarespace.template.Instruction in project template-compiler by Squarespace.
the class ContentFormattersTest method testCapitalize.
@Test
public void testCapitalize() throws CodeException {
String template = "{user.name|capitalize}";
String json = "{\"user\": {\"name\": \"Bob Smith\"}}";
Instruction code = compiler().compile(template).code();
Context ctx = compiler().newExecutor().code(code).json(json).execute();
String result = eval(ctx);
assertEquals(result, "BOB SMITH");
}
use of com.squarespace.template.Instruction in project template-compiler by Squarespace.
the class ContentFormattersTest method testAbsUrl.
@Test
public void testAbsUrl() throws CodeException {
String template = "{a|AbsUrl}";
String json = "{\"base-url\": \"http://foobar.com/foo\", \"a\": \"abc\"}";
Instruction code = compiler().compile(template).code();
Context ctx = compiler().newExecutor().code(code).json(json).execute();
String result = eval(ctx);
assertEquals(result, "http://foobar.com/foo/abc");
}
use of com.squarespace.template.Instruction in project template-compiler by Squarespace.
the class TemplateC method compile.
/**
* Compile a template against a given json tree and emit the result.
*/
protected int compile(String templatePath, String jsonPath, String partialsPath, String locale, boolean preprocess) throws CodeException, IOException {
String template = readFile(templatePath);
String json = "{}";
if (jsonPath != null) {
json = readFile(jsonPath);
}
String partials = null;
if (partialsPath != null) {
partials = readFile(partialsPath);
}
if (locale == null) {
locale = "en-US";
}
CompiledTemplate compiled = compiler().compile(template, true, preprocess);
StringBuilder errorBuf = new StringBuilder();
List<ErrorInfo> errors = compiled.errors();
if (!errors.isEmpty()) {
errorBuf.append("Caught errors executing template:\n");
for (ErrorInfo error : errors) {
errorBuf.append(" ").append(error.getMessage()).append('\n');
}
}
Instruction code = compiled.code();
// Parse the JSON context
JsonNode jsonTree = null;
try {
jsonTree = JsonUtils.decode(json);
} catch (IllegalArgumentException e) {
System.err.println("Caught error trying to parse JSON: " + e.getCause().getMessage());
return 1;
}
// Parse the optional JSON partials dictionary.
JsonNode partialsTree = null;
if (partials != null) {
try {
partialsTree = JsonUtils.decode(partials);
if (!(partialsTree instanceof ObjectNode)) {
System.err.println("Partials map JSON must be an object. Found " + partialsTree.getNodeType());
return 1;
}
} catch (IllegalArgumentException e) {
System.err.println("Caught error trying to parse partials: " + e.getCause().getMessage());
return 1;
}
}
// Perform the compile.
Context context = compiler().newExecutor().code(code).json(jsonTree).locale(Locale.forLanguageTag(locale)).safeExecution(true).partialsMap((ObjectNode) partialsTree).enableExpr(true).enableInclude(true).execute();
// If compile was successful, print the output.
System.out.print(context.buffer().toString());
if (errorBuf.length() > 0) {
System.err.println(errorBuf.toString());
}
return 0;
}
Aggregations