Search in sources :

Example 1 with ImmutableDefaultFactory

use of org.mvel2.integration.impl.ImmutableDefaultFactory in project vertx-web by vert-x3.

the class MVELTemplateEngineImpl method render.

@Override
public void render(RoutingContext context, String templateDirectory, String templateFileName, Handler<AsyncResult<Buffer>> handler) {
    try {
        templateFileName = templateDirectory + templateFileName;
        CompiledTemplate template = isCachingEnabled() ? cache.get(templateFileName) : null;
        if (template == null) {
            // real compile
            String loc = adjustLocation(templateFileName);
            String templateText = Utils.readFileToString(context.vertx(), loc);
            if (templateText == null) {
                throw new IllegalArgumentException("Cannot find template " + loc);
            }
            template = TemplateCompiler.compileTemplate(templateText);
            if (isCachingEnabled()) {
                cache.put(templateFileName, template);
            }
        }
        Map<String, RoutingContext> variables = new HashMap<>(1);
        variables.put("context", context);
        final VertxInternal vertxInternal = (VertxInternal) context.vertx();
        String directoryName = vertxInternal.resolveFile(templateFileName).getParent();
        handler.handle(Future.succeededFuture(Buffer.buffer((String) new TemplateRuntime(template.getTemplate(), null, template.getRoot(), directoryName).execute(new StringAppender(), variables, new ImmutableDefaultFactory()))));
    } catch (Exception ex) {
        handler.handle(Future.failedFuture(ex));
    }
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) VertxInternal(io.vertx.core.impl.VertxInternal) ImmutableDefaultFactory(org.mvel2.integration.impl.ImmutableDefaultFactory) HashMap(java.util.HashMap) StringAppender(org.mvel2.util.StringAppender) TemplateRuntime(org.mvel2.templates.TemplateRuntime) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Example 2 with ImmutableDefaultFactory

use of org.mvel2.integration.impl.ImmutableDefaultFactory in project mvel by mvel.

the class MVELIncludeTest method testEvalFile1.

/**
 * evalFile with sub template in utf-8 encoding
 *
 * @throws IOException
 */
public void testEvalFile1() throws IOException {
    final CompiledTemplate template = TemplateCompiler.compileTemplate(file);
    final String tr = (String) new TemplateRuntime(template.getTemplate(), null, template.getRoot(), "./samples/scripts/").execute(new StringAppender(), new HashMap<String, String>(), new ImmutableDefaultFactory());
    assertEquals("Hello mister Gaël Périé", tr);
}
Also used : ImmutableDefaultFactory(org.mvel2.integration.impl.ImmutableDefaultFactory) HashMap(java.util.HashMap) StringAppender(org.mvel2.util.StringAppender) TemplateRuntime(org.mvel2.templates.TemplateRuntime) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

HashMap (java.util.HashMap)2 ImmutableDefaultFactory (org.mvel2.integration.impl.ImmutableDefaultFactory)2 CompiledTemplate (org.mvel2.templates.CompiledTemplate)2 TemplateRuntime (org.mvel2.templates.TemplateRuntime)2 StringAppender (org.mvel2.util.StringAppender)2 VertxInternal (io.vertx.core.impl.VertxInternal)1 RoutingContext (io.vertx.ext.web.RoutingContext)1