Search in sources :

Example 1 with HandlebarsTemplateEngine

use of io.vertx.ext.web.templ.HandlebarsTemplateEngine in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // To simplify the development of the web components we use a Router to route all HTTP requests
    // to organize our code in a reusable way.
    final Router router = Router.router(vertx);
    // In order to use a template we first need to create an engine
    final HandlebarsTemplateEngine engine = HandlebarsTemplateEngine.create();
    // Entry point to the application, this will render a custom template.
    router.get().handler(ctx -> {
        // we define a hardcoded title for our application
        ctx.put("title", "Seasons of the year");
        // we define a hardcoded array of json objects
        JsonArray seasons = new JsonArray();
        seasons.add(new JsonObject().put("name", "Spring"));
        seasons.add(new JsonObject().put("name", "Summer"));
        seasons.add(new JsonObject().put("name", "Autumn"));
        seasons.add(new JsonObject().put("name", "Winter"));
        ctx.put("seasons", seasons);
        // and now delegate to the engine to render it.
        engine.render(ctx, "templates/index.hbs", res -> {
            if (res.succeeded()) {
                ctx.response().end(res.result());
            } else {
                ctx.fail(res.cause());
            }
        });
    });
    // start a HTTP web server on port 8080
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) HandlebarsTemplateEngine(io.vertx.ext.web.templ.HandlebarsTemplateEngine)

Example 2 with HandlebarsTemplateEngine

use of io.vertx.ext.web.templ.HandlebarsTemplateEngine in project vertx-examples by vert-x3.

the class Http2ServerVerticle method createRouter.

private Router createRouter(String redirectURL) {
    Router router = Router.router(vertx);
    HandlebarsTemplateEngine engine = HandlebarsTemplateEngine.create();
    engine.setMaxCacheSize(0);
    router.get("/*").handler(rc -> {
        int queryLatency = DEFAULT_LATENCY;
        try {
            queryLatency = Integer.valueOf(rc.request().getParam("latency"));
        } catch (NumberFormatException nfe) {
        }
        rc.put("query-latency", queryLatency);
        if (queryLatency == 0) {
            rc.next();
        } else {
            vertx.setTimer(queryLatency, id -> rc.next());
        }
    });
    router.getWithRegex(".+\\.hbs").handler(ctx -> {
        final Stream<Integer> availableLatencies = Stream.of(0, 20, 40, 60, 80, 100);
        Integer queryLatency = ctx.get("query-latency");
        ctx.put("imgs", createImages(queryLatency));
        ctx.put("tileHeight", TILE_HEIGHT);
        ctx.put("tileWidth", TILE_WIDTH);
        ctx.put("host", host);
        ctx.put("http1Port", HTTP1_PORT);
        ctx.put("http2Port", HTTP2_PORT);
        Stream<DisplayedLatency> displayedLatencies = availableLatencies.map(latency -> new DisplayedLatency(latency, queryLatency));
        ctx.put("latencies", displayedLatencies.collect(Collectors.toList()));
        ctx.next();
    });
    router.getWithRegex(".+\\.hbs").handler(TemplateHandler.create(engine));
    router.get("/assets/*").handler(StaticHandler.create());
    router.get("/").handler(ctx -> {
        ctx.response().setStatusCode(302).putHeader("Location", redirectURL).end();
    });
    return router;
}
Also used : Router(io.vertx.ext.web.Router) HandlebarsTemplateEngine(io.vertx.ext.web.templ.HandlebarsTemplateEngine)

Aggregations

Router (io.vertx.ext.web.Router)2 HandlebarsTemplateEngine (io.vertx.ext.web.templ.HandlebarsTemplateEngine)2 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1