examples: annotate examples
[nit.git] / lib / popcorn / pop_templates.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2017 Alexandre Terrasa <alexandre@moz-code.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Template rendering for popcorn
18 #
19 # ## Basic templates
20 #
21 # Use TemplateString to render really basic templates that just need macro
22 # replacements.
23 #
24 # Example:
25 # ~~~nit
26 # class TemplateStringHandler
27 # super Handler
28 #
29 # redef fun get(req, res) do
30 # # Values to add in the template
31 # var values = new HashMap[String, String]
32 # values["USER"] = "Morriar"
33 # values["MYSITE"] = "My super website"
34 #
35 # # Render it with a shortcut
36 # res.template_string("""
37 # <h1>Hello %USER%!</h1>
38 # <p>Welcome to %MYSITE%.</p>
39 # """, values)
40 # end
41 # end
42 # ~~~
43 #
44 # For larger templates, you can also use external files (makes your Nit code cleaner):
45 # ~~~nit
46 # class TemplateFileHandler
47 # super Handler
48 #
49 # redef fun get(req, res) do
50 # # Values to add in the template
51 # var values = new HashMap[String, String]
52 # values["USER"] = "Morriar"
53 # values["MYSITE"] = "My super website"
54 #
55 # # Render it from an external file
56 # res.template_file("example_template.tpl", values)
57 # end
58 # end
59 # ~~~
60 #
61 # ## Using pug templates
62 #
63 # Pug is a templating format provided by the external command `pug`.
64 # For complex templates that need conditional or loop statements, pug can be a solution.
65 #
66 # See the pug syntax here: https://pugjs.org/api/getting-started.html
67 #
68 # ~~~nit
69 # class PugFileHandler
70 # super Handler
71 #
72 # redef fun get(req, res) do
73 # # Values to add in the template
74 # var json = new JsonObject
75 # json["user"] = "Morriar"
76 # json["mysite"] = "My super website"
77 #
78 # # Render it from an external file
79 # res.pug_file("example_template.pug", json)
80 # end
81 # end
82 # ~~~
83 module pop_templates
84
85 import popcorn::pop_handlers
86 import popcorn::pop_json
87 import template::macro
88
89 redef class HttpResponse
90
91 # Render the TemplateString `tpl` with `values` and write it as html.
92 fun template(tpl: nullable TemplateString, values: nullable Map[String, nullable Writable], status: nullable Int) do
93 if tpl != null and values != null then
94 for k, v in values do
95 if not tpl.has_macro(k) or v == null then continue
96 tpl.replace(k, v)
97 end
98 end
99 html(tpl, status)
100 end
101
102 # Render `file` as a TemplateString with `values` and write it as html.
103 fun template_string(tpl_string: nullable String, values: nullable Map[String, nullable Writable], status: nullable Int) do
104 var tpl = new TemplateString(tpl_string or else "")
105 template(tpl, values, status)
106 end
107
108 # Render `file` as a TemplateString with `values` and write it as html.
109 fun template_file(file: String, values: nullable Map[String, nullable Writable], status: nullable Int) do
110 var tpl = new TemplateString.from_file(file)
111 template(tpl, values, status)
112 end
113
114 # Render `pug_string` with the command cli `pug` and use data from `json`
115 #
116 # See https://pugjs.org/api/getting-started.html for more details on pug.
117 fun pug(pug_string: nullable String, json: nullable Serializable, status: nullable Int) do
118 var process
119 if json == null then
120 process = new ProcessDuplex("pug", "-D")
121 else
122 process = new ProcessDuplex("pug", "-D", "-O", json.to_json)
123 end
124 var out = process.write_and_read(pug_string or else "")
125 process.close
126 html(out, status)
127 end
128
129 # Render `file` with the command cli `pug` and use data from `json`
130 #
131 # See https://pugjs.org/api/getting-started.html for more details on pug.
132 fun pug_file(file: String, json: nullable Serializable, status: nullable Int) do
133 pug(file.to_path.read_all, json, status)
134 end
135 end