package examples.templating;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.Layout;
import com.vtence.molecule.routing.DynamicRoutes;
import com.vtence.molecule.templating.JMustacheRenderer;
import com.vtence.molecule.templating.Template;
import com.vtence.molecule.templating.Templates;

import java.io.IOException;
import java.util.Map;

import static com.vtence.molecule.testing.ResourceLocator.locateOnClasspath;

public class TemplatingAndLayoutExample {

    public void run(WebServer server) throws IOException {
        // We use Mustache templates with an .html extension
        Templates templates = new Templates(
                new JMustacheRenderer().fromDir(locateOnClasspath("examples/templates")).extension("html"));
        final Template<Map<String, String>> layout = templates.named("layout");
        final Template<User> greeting = templates.named("greeting");

        // Apply a common layout to all rendered pages
        server.filter("/", Layout.html(layout))
              .start(new DynamicRoutes() {{
                  get("/hello").to((request, response) -> {
                      response.contentType("text/html; charset=utf-8");
                      String name = request.parameter("name") != null ? request.parameter("name") : "World";
                      // Mustache can use any object or a Map as a rendering context
                      response.done(greeting.render(new User(name)));
                  });
              }});
    }

    private static class User {
        public final String name;

        public User(String name) {
            this.name = name;
        }
    }

    public static void main(String[] args) throws IOException {
        TemplatingAndLayoutExample example = new TemplatingAndLayoutExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri() + "/hello?name=Gandalf");
    }
}
@@ -442,16 +442,339 @@
 spath;%0A%0A
+/**%0A * %3Cp%3E%0A * In this example we demonstrate rendering output using a template engine. We have a template for a common site%0A * layout and templates for the individual pages.%0A * %3C/p%3E%0A * %3Cp%3E%0A * Our templates are Mustache templates with an %3Ccode%3E.html%3C/code%3E extension. We use JMustache as the template%0A * engine.%0A * %3C/p%3E%0A */%0A
 public c
@@ -885,16 +885,36 @@
  We use 
+JMustache to render 
 Mustache
@@ -1098,24 +1098,86 @@
 n(%22html%22));%0A
+        // This is template for the shared application layout%0A
         fina
@@ -1240,24 +1240,83 @@
 (%22layout%22);%0A
+        // This is the template for the greeting.html page%0A
         fina
@@ -1399,22 +1399,62 @@
  common 
+site 
 layout
+ to requests under the / path, i.e.
  to all 

Add javadoc to the template and layout example