lib/dot: rename `exemples` to `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 template::macro
87
88 redef class HttpResponse
89
90 # Render the TemplateString `tpl` with `values` and write it as html.
91 fun template(tpl: nullable TemplateString, values: nullable Map[String, nullable Writable], status: nullable Int) do
92 if tpl != null and values != null then
93 for k, v in values do
94 if not tpl.has_macro(k) or v == null then continue
95 tpl.replace(k, v)
96 end
97 end
98 html(tpl, status)
99 end
100
101 # Render `file` as a TemplateString with `values` and write it as html.
102 fun template_string(tpl_string: nullable String, values: nullable Map[String, nullable Writable], status: nullable Int) do
103 var tpl = new TemplateString(tpl_string or else "")
104 template(tpl, values, status)
105 end
106
107 # Render `file` as a TemplateString with `values` and write it as html.
108 fun template_file(file: String, values: nullable Map[String, nullable Writable], status: nullable Int) do
109 var tpl = new TemplateString.from_file(file)
110 template(tpl, values, status)
111 end
112
113 # Render `pug_string` with the command cli `pug` and use data from `json`
114 #
115 # See https://pugjs.org/api/getting-started.html for more details on pug.
116 fun pug(pug_string: nullable String, json: nullable Serializable, status: nullable Int) do
117 var process
118 if json == null then
119 process = new ProcessDuplex("pug", "-D")
120 else
121 process = new ProcessDuplex("pug", "-D", "-O", json.to_json)
122 end
123 var out = process.write_and_read(pug_string or else "")
124 process.close
125 html(out, status)
126 end
127
128 # Render `file` with the command cli `pug` and use data from `json`
129 #
130 # See https://pugjs.org/api/getting-started.html for more details on pug.
131 fun pug_file(file: String, json: nullable Serializable, status: nullable Int) do
132 pug(file.to_path.read_all, json, status)
133 end
134 end