Search in sources :

Example 6 with Instruction

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);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 7 with Instruction

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);
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 8 with Instruction

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");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 9 with Instruction

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");
}
Also used : Context(com.squarespace.template.Context) Instruction(com.squarespace.template.Instruction) Test(org.testng.annotations.Test)

Example 10 with Instruction

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;
}
Also used : Context(com.squarespace.template.Context) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ErrorInfo(com.squarespace.template.ErrorInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) Instruction(com.squarespace.template.Instruction) CompiledTemplate(com.squarespace.template.CompiledTemplate)

Aggregations

Context (com.squarespace.template.Context)14 Instruction (com.squarespace.template.Instruction)14 Test (org.testng.annotations.Test)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Arguments (com.squarespace.template.Arguments)1 CodeBuilder (com.squarespace.template.CodeBuilder)1 CodeExecuteException (com.squarespace.template.CodeExecuteException)1 CodeMaker (com.squarespace.template.CodeMaker)1 CompiledTemplate (com.squarespace.template.CompiledTemplate)1 ErrorInfo (com.squarespace.template.ErrorInfo)1