@Override public void start() { Router router = Router.router(vertx); // Serve the dynamic pages router.route("/dynamic/*") .handler(ctx -> { // put the context into the template render context ctx.put("context", ctx); ctx.next(); }) .handler(TemplateHandler.create(MVELTemplateEngine.create(vertx))); // Serve the static pages router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router).listen(8080); }
/** * Create a template engine using defaults * @param vertx * @return the engine */ public static io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine create(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine ret = io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine.newInstance(io.vertx.ext.web.templ.mvel.MVELTemplateEngine.create(vertx.getDelegate())); return ret; }
/** * Create a template engine using defaults * @param vertx * @return the engine */ public static io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine create(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine ret = io.vertx.rxjava.ext.web.templ.mvel.MVELTemplateEngine.newInstance(io.vertx.ext.web.templ.mvel.MVELTemplateEngine.create(vertx.getDelegate())); return ret; }
@Test public void MVELTemplateTestMVELTemplateTestMVELTemplateTest(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox"); context.put("context", new JsonObject().put("path", "/test-mvel-template3.templ")); engine.render(context, "src/test/filesystemtemplates/test-mvel-template3.templ", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /test-mvel-template3.templ\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerWithInclude(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox"); context.put("context", new JsonObject().put("path", "/test-mvel-template4.templ")); engine.render(context, "src/test/filesystemtemplates/test-mvel-template4.templ", render -> { should.assertTrue(render.succeeded()); String res = render.result().toString(); should.assertEquals("Hello badger and fox\n\nRequest path is /test-mvel-template4.templ\n", res); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerOnClasspath(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox"); context.put("context", new JsonObject().put("path", "/test-mvel-template2.templ")); engine.render(context, "somedir/test-mvel-template2.templ", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /test-mvel-template2.templ\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerNoExtension(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox"); context.put("context", new JsonObject().put("path", "/test-mvel-template2.templ")); engine.render(context, "somedir/test-mvel-template2", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /test-mvel-template2.templ\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerChangeExtension(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx).setExtension("bempl"); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox"); context.put("context", new JsonObject().put("path", "/test-mvel-template2")); engine.render(context, "somedir/test-mvel-template2", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Cheerio badger and fox\nRequest path is /test-mvel-template2\n", render.result().toString()); test.complete(); }); test.await(); }
TemplateEngine engine = MVELTemplateEngine.create(vertx);
TemplateEngine engine = MVELTemplateEngine.create(vertx);
@Test public void testNoSuchTemplate(TestContext should) { final Async test = should.async(); TemplateEngine engine = MVELTemplateEngine.create(vertx); engine.render(new JsonObject(), "nosuchtemplate.templ", render -> { should.assertFalse(render.succeeded()); test.complete(); }); test.await(); }