1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2017 Alexandre Terrasa <alexandre@moz-code.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Template rendering for popcorn
21 # Use TemplateString to render really basic templates that just need macro
26 # class TemplateStringHandler
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"
35 # # Render it with a shortcut
36 # res.template_string("""
37 # <h1>Hello %USER%!</h1>
38 # <p>Welcome to %MYSITE%.</p>
44 # For larger templates, you can also use external files (makes your Nit code cleaner):
46 # class TemplateFileHandler
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"
55 # # Render it from an external file
56 # res.template_file("example_template.tpl", values)
61 # ## Using pug templates
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.
66 # See the pug syntax here: https://pugjs.org/api/getting-started.html
69 # class PugFileHandler
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"
78 # # Render it from an external file
79 # res.pug_file("example_template.pug", json)
85 import popcorn
::pop_handlers
86 import popcorn
::pop_json
87 import template
::macro
89 redef class HttpResponse
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
95 if not tpl
.has_macro
(k
) or v
== null then continue
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
)
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
)
114 # Render `pug_string` with the command cli `pug` and use data from `json`
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
120 process
= new ProcessDuplex("pug", "-D")
122 process
= new ProcessDuplex("pug", "-D", "-O", json
.to_json
)
124 var out
= process
.write_and_read
(pug_string
or else "")
129 # Render `file` with the command cli `pug` and use data from `json`
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
)