@Override public void start() throws Exception { final Router router = Router.router(vertx); // Populate context with data router.route().handler(ctx -> { ctx.put("title", "Vert.x Web Example Using Rocker"); ctx.put("name", "Rocker"); ctx.next(); }); // Render a custom template. // Note: you need a compile-time generator for Rocker to work properly // See the pom.xml for an example router.route().handler(TemplateHandler.create(RockerTemplateEngine.create())); vertx.createHttpServer().requestHandler(router).listen(8080); } }
/** * @return the engine */ public static io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine create() { io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine ret = io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine.newInstance(io.vertx.ext.web.templ.rocker.RockerTemplateEngine.create()); return ret; }
/** * @return the engine */ public static io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine create() { io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine ret = io.vertx.rxjava.ext.web.templ.rocker.RockerTemplateEngine.newInstance(io.vertx.ext.web.templ.rocker.RockerTemplateEngine.create()); return ret; }
@Test public void testTemplateHandler(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create(); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox") .put("context", new JsonObject().put("path", "/TestRockerTemplate2.rocker.html")); engine.render(context, "somedir/TestRockerTemplate2.rocker.html", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2.rocker.html\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerIncludes(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create(); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox") .put("context", new JsonObject().put("path", "/TestRockerTemplate3")); engine.render(context, "somedir/Base", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Vert.x rules\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateHandlerNoExtension(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create(); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox") .put("context", new JsonObject().put("path", "/TestRockerTemplate2")); engine.render(context, "somedir/TestRockerTemplate2", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testTemplateWithUndrescoreKeysHandler(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create(); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox") .put("context", new JsonObject().put("path", "/TestRockerTemplate2.rocker.html")) .put("__body-handled", true); engine.render(context, "somedir/TestRockerTemplate2.rocker.html", render -> { should.assertTrue(render.succeeded()); should.assertEquals("Hello badger and fox\nRequest path is /TestRockerTemplate2.rocker.html\n", render.result().toString()); test.complete(); }); test.await(); } }
@Test public void testTemplateHandlerChangeExtension(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create().setExtension("rocker.raw"); final JsonObject context = new JsonObject() .put("foo", "badger") .put("bar", "fox") .put("context", new JsonObject().put("path", "/TestRockerTemplate3")); engine.render(context, "somedir/TestRockerTemplate3", render -> { should.assertTrue(render.succeeded()); should.assertEquals("\nCheerio badger and fox\nRequest path is /TestRockerTemplate3\n", render.result().toString()); test.complete(); }); test.await(); }
@Test public void testNoSuchTemplate(TestContext should) { final Async test = should.async(); TemplateEngine engine = RockerTemplateEngine.create(); final JsonObject context = new JsonObject(); engine.render(context, "nosuchtemplate.rocker.html", render -> { should.assertFalse(render.succeeded()); test.complete(); }); test.await(); }