examples: annotate examples
[nit.git] / lib / popcorn / examples / templates / example_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 module example_templates is example
18
19 import popcorn
20 import popcorn::pop_templates
21
22 class MyTemplateHandler
23 super Handler
24
25 # Template to render
26 var template = new TemplateString("""
27 <h1>Hello %USER%!</h1>
28 <p>Welcome to %MYSITE%.</p>
29 """)
30
31 redef fun get(req, res) do
32 # Values to add in the template
33 var values = new HashMap[String, String]
34 values["USER"] = "Morriar"
35 values["MYSITE"] = "My super website"
36
37 # Render it
38 res.template(template, values)
39 end
40 end
41
42 class MyTemplateStringHandler
43 super Handler
44
45 redef fun get(req, res) do
46 # Values to add in the template
47 var values = new HashMap[String, String]
48 values["USER"] = "Morriar"
49 values["MYSITE"] = "My super website"
50
51 # Render it with a shortcut
52 res.template_string("""
53 <h1>Hello %USER%!</h1>
54 <p>Welcome to %MYSITE%.</p>
55 """, values)
56 end
57 end
58
59 class MyTemplateFileHandler
60 super Handler
61
62 # Use template from external file
63 var tpl_file = "example_template.tpl"
64
65 redef fun get(req, res) do
66 # Values to add in the template
67 var values = new HashMap[String, String]
68 values["USER"] = "Morriar"
69 values["MYSITE"] = "My super website"
70
71 # Render it from an external file
72 res.template_file(tpl_file, values)
73 end
74 end
75
76 class MyTemplatePugHandler
77 super Handler
78
79 redef fun get(req, res) do
80 # Values to add in the template
81 var json = new JsonObject
82 json["user"] = "Morriar"
83 json["mysite"] = "My super website"
84
85 res.pug("""
86 h1 Hello #{user}
87 p Welcome to #{mysite}
88 """, json)
89 end
90 end
91
92 class MyTemplatePugFileHandler
93 super Handler
94
95 # Use template from external file
96 var pug_file = "example_template.pug"
97
98 redef fun get(req, res) do
99 # Values to add in the template
100 var json = new JsonObject
101 json["user"] = "Morriar"
102 json["mysite"] = "My super website"
103
104 # Render it from an external file
105 res.pug_file(pug_file, json)
106 end
107 end
108
109 var app = new App
110 app.use("/", new MyTemplateHandler)
111 app.use("/string", new MyTemplateStringHandler)
112 app.use("/file", new MyTemplateFileHandler)
113 app.use("/pug", new MyTemplatePugHandler)
114 app.use("/pug_file", new MyTemplatePugFileHandler)
115 app.listen("localhost", 3000)