Merge: Popcorn templates
authorJean Privat <jean@pryen.org>
Thu, 25 May 2017 14:55:23 +0000 (10:55 -0400)
committerJean Privat <jean@pryen.org>
Thu, 25 May 2017 14:55:23 +0000 (10:55 -0400)
commitc98d24a3ff300d43a882a1db7ad93e4e684d9ccc
tree5178ba8732a8bdac17d6da48e6d5dc3fb59b64b0
parente7880d5f7f14ebf0ad910fe268d60baec6138206
parentb70cc0052a9f5d34884a8450349cc6c771cfc23d
Merge: Popcorn templates

# Template rendering for popcorn

## Basic templates

Use TemplateString to render really basic templates that just need macro
replacements.

Example:
~~~nit
class TemplateStringHandler
        super Handler
redef fun get(req, res) do
# Values to add in the template
var values = new HashMap[String, String]
values["USER"] = "Morriar"
values["MYSITE"] = "My super website"

# Render it with a shortcut
res.template_string("""
<h1>Hello %USER%!</h1>
<p>Welcome to %MYSITE%.</p>
""", values)
end
 end
 ~~~

 For larger templates, you can also use external files (makes your Nit code cleaner):
 ~~~nit
 class TemplateFileHandler
super Handler

redef fun get(req, res) do
# Values to add in the template
var values = new HashMap[String, String]
values["USER"] = "Morriar"
values["MYSITE"] = "My super website"

# Render it from an external file
res.template_file("example_template.tpl", values)
end
 end
 ~~~

 ## Using pug templates

 Pug is a templating format provided by the external command `pug`.
 For complex templates that need conditional or loop statements, pug can be a solution.

 See the pug syntax here: https://pugjs.org/api/getting-started.html

 ~~~nit
 class PugFileHandler
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"

# Render it from an external file
res.pug_file("example_template.pug", json)
end
 end
 ~~~

Pull-Request: #2449
Reviewed-by: Jean Privat <jean@pryen.org>