Java源码示例:io.vertx.ext.web.common.template.TemplateEngine

示例1
private TemplateEngine createTemplateEngine(String templateType) {
    Vertx vertx;
    try {
        vertx = beanFactory.getBean(Vertx.class);
    } catch (BeansException e) {
        throw new IllegalStateException(e);
    }

    TemplateEngine templateEngine;
    if ("freemarker".equals(templateType)) {
        templateEngine = FreeMarkerTemplateEngine.create(vertx);
        return templateEngine;
    }

    if ("jade".equals(templateType)) {
        templateEngine = JadeTemplateEngine.create(vertx);
        return templateEngine;
    }

    if ("thymeleaf".equals(templateType)) {
        templateEngine = ThymeleafTemplateEngine.create(vertx);
        return templateEngine;
    }

    throw new IllegalStateException(String.format("unsupported type of template %s!", templateType));
}
 
示例2
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/test-freemarker-template2.ftl", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例3
@Test
public void testTemplateHandlerOnFileSystem(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template3.ftl"));

  engine.render(context, "src/test/filesystemtemplates/test-freemarker-template3.ftl", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template3.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例4
@Test
public void testTemplateHandlerNoExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/test-freemarker-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例5
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx, "mvl");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/test-freemarker-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Cheerio badger and fox\nRequest path is /test-freemarker-template2.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例6
@Test
public void testTemplateHandlerIncludes(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/base", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Vert.x rules", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例7
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HTTLTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "somedir/test-httl-template1.httl", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例8
@Test
public void testTemplateHandlerNoExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HTTLTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "somedir/test-httl-template1", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例9
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HTTLTemplateEngine.create(vertx, "mvl");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "somedir/test-httl-template1", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Cheerio badger and fox\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例10
@Test
public void testTemplateOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "somedir/test-handlebars-template2.hbs", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例11
@Test
public void testTemplateJsonArrayResolver(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);

  JsonArray jsonArray = new JsonArray();
  jsonArray.add("badger").add("fox").add(new JsonObject().put("name", "joe"));
  String expected = "Iterator: badger,fox,{"name":"joe"}, Element by index:fox - joe - Out of bounds:  - Size:3";

  engine.render(new JsonObject().put("foo", jsonArray), "src/test/filesystemtemplates/test-handlebars-template5.hbs", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals(expected, render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例12
@Test
public void testTemplatePerf(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  final AtomicInteger cnt = new AtomicInteger(0);
  final long t0 = System.currentTimeMillis();
  for (int i = 0; i < 1000000; i++) {
    engine.render(context, "somedir/test-handlebars-template2.hbs", render -> {
      should.assertTrue(render.succeeded());
      should.assertEquals("Hello badger and fox", render.result().toString());
      if (cnt.incrementAndGet() == 1000000) {
        final long t1 = System.currentTimeMillis();
        System.out.println(t1 - t0);
        test.complete();
      }
    });
  }
}
 
示例13
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template2.peb"));

  engine.render(context, "somedir/test-pebble-template2.peb", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and foxRequest path is /test-pebble-template2.peb", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例14
@Test
public void testTemplateHandlerOnFileSystem(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template3.peb"));

  engine.render(context, "src/test/filesystemtemplates/test-pebble-template3.peb", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and foxRequest path is /test-pebble-template3.peb", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例15
@Test
public void testTemplateHandlerNoExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template2.peb"));

  engine.render(context, "somedir/test-pebble-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and foxRequest path is /test-pebble-template2.peb", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例16
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx, "beb");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template2.peb"));

  engine.render(context, "somedir/test-pebble-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Cheerio badger and foxRequest path is /test-pebble-template2.peb", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例17
@Test
public void testTemplateComplex(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx);

  String expected = "Hello.Hi fox.\nHi badger!\nFooter - badger";

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "src/test/filesystemtemplates/test-pebble-complex.peb", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals(expected, render.result().toString());
    test.complete();
  });
}
 
示例18
@Test
public void customBuilderShouldRender(TestContext should) {
  final Async test = should.async();
  final TemplateEngine engine = PebbleTemplateEngine.create(vertx, new PebbleEngine.Builder().extension(new TestExtension()).loader(new PebbleVertxLoader(vertx)).build());

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template5.peb"));

  engine.render(context, "src/test/filesystemtemplates/test-pebble-template5.peb", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and foxString is TESTRequest path is /test-pebble-template5.peb", render.result().toString());
    test.complete();
  });
}
 
示例19
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = JadeTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-jade-template2.jade"));

  engine.render(context, "somedir/test-jade-template2.jade", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("<!DOCTYPE html><html><head><title>badger/test-jade-template2.jade</title></head><body></body></html>", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例20
@Test
public void testTemplateHandlerOnFileSystem(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = JadeTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-jade-template3.jade"));

  engine.render(context, "src/test/filesystemtemplates/test-jade-template3.jade", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("<!DOCTYPE html><html><head><title>badger/test-jade-template3.jade</title></head><body></body></html>", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例21
@Test
public void testTemplateHandlerNoExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = JadeTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-jade-template2.jade"));

  engine.render(context, "somedir/test-jade-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("<!DOCTYPE html><html><head><title>badger/test-jade-template2.jade</title></head><body></body></html>", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例22
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = JadeTemplateEngine.create(vertx, "made");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-jade-template2.jade"));

  engine.render(context, "somedir/test-jade-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("<!DOCTYPE html><html><head><title>aardvark/test-jade-template2.jade</title></head><body></body></html>", render.result().toString());
    test.complete();
  });
  test.await();
}
 
示例23
@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();
}
 
示例24
@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();
}
 
示例25
@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();
}
 
示例26
@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();
}
 
示例27
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = MVELTemplateEngine.create(vertx, "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();
}
 
示例28
@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();
}
 
示例29
@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();
}
 
示例30
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = RockerTemplateEngine.create("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();
}