--- /dev/null
+h1 Hello #{user}!
+p Welcome on #{mysite}.
end
end
+class MyTemplatePugHandler
+ super Handler
+
+ redef fun get(req, res) do
+ # Values to add in the template
+ var json = new JsonObject
+ json["user"] = "Morriar"
+ json["mysite"] = "My super website"
+
+ res.pug("""
+h1 Hello #{user}
+p Welcome to #{mysite}
+ """, json)
+ end
+end
+
+class MyTemplatePugFileHandler
+ super Handler
+
+ # Use template from external file
+ var pug_file = "example_template.pug"
+
+ redef fun get(req, res) do
+ # Values to add in the template
+ var json = new JsonObject
+ json["user"] = "Morriar"
+ json["mysite"] = "My super website"
+
+ # Render it from an external file
+ res.pug_file(pug_file, json)
+ end
+end
+
var app = new App
app.use("/", new MyTemplateHandler)
app.use("/string", new MyTemplateStringHandler)
app.use("/file", new MyTemplateFileHandler)
+app.use("/pug", new MyTemplatePugHandler)
+app.use("/pug_file", new MyTemplatePugFileHandler)
app.listen("localhost", 3000)
--- /dev/null
+
+[Client] curl -s localhost:*****/
+<h1>Hello Morriar</h1><p>Welcome to My super website</p>
--- /dev/null
+
+[Client] curl -s localhost:*****/
+<h1>Hello Morriar!</h1><p>Welcome on My super website.</p>
run_test(app)
end
end
+
+class TestExampleTemplatePug
+ super TestPopcorn
+
+ redef fun client_test do
+ system "curl -s {host}:{port}/"
+ end
+
+ fun test_example_template_pug do
+ var app = new App
+ app.use("/", new MyTemplatePugHandler)
+ run_test(app)
+ end
+end
+
+class TestExampleTemplatePugFile
+ super TestPopcorn
+
+ redef fun client_test do
+ system "curl -s {host}:{port}/"
+ end
+
+ fun test_example_template_pug_file do
+ var app = new App
+ var handler = new MyTemplatePugFileHandler
+ handler.pug_file = test_path / "../example_template.pug"
+ app.use("/", handler)
+ run_test(app)
+ end
+end
var tpl = new TemplateString.from_file(file)
template(tpl, values, status)
end
+
+ # Render `pug_string` with the command cli `pug` and use data from `json`
+ #
+ # See https://pugjs.org/api/getting-started.html for more details on pug.
+ fun pug(pug_string: nullable String, json: nullable Serializable, status: nullable Int) do
+ var process
+ if json == null then
+ process = new ProcessDuplex("pug", "-D")
+ else
+ process = new ProcessDuplex("pug", "-D", "-O", json.to_json)
+ end
+ var out = process.write_and_read(pug_string or else "")
+ process.close
+ html(out, status)
+ end
+
+ # Render `file` with the command cli `pug` and use data from `json`
+ #
+ # See https://pugjs.org/api/getting-started.html for more details on pug.
+ fun pug_file(file: String, json: nullable Serializable, status: nullable Int) do
+ pug(file.to_path.read_all, json, status)
+ end
end