-
Notifications
You must be signed in to change notification settings - Fork 4
/
t2ws_template.tcl
257 lines (228 loc) · 8.77 KB
/
t2ws_template.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
##########################################################################
# T2WS - Tiny Tcl Web Server
##########################################################################
# t2ws_template.tcl - Templating engine plugin for T2WS
#
# This file implements a templating engine for the T2WS web server
#
# Copyright (C) 2016 Andreas Drollinger
##########################################################################
# See the file "LICENSE" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
##########################################################################
# Title: T2WS Template - Template engine for T2WS
#
# Group: Introduction
# This plugin extends the T2WS server with a template engine that process
# template files or raw response data to generate the desired output file
# (usually a HTML page). The plugin is loaded and enabled by executing the
# following commands :
#
# > package require t2ws::template
# > t2ws::EnablePlugin $Port t2ws::template
#
# Once loaded the responder command can add the following response dictionary
# field to trigger the T2WS server to post-process the response data by
# running the template engine.
#
# IsTemplate - If the value of this field is true (e.g. 1) the T2WS
# web server will run the template engine to post-process the
# HTTP response body before sending it to the HTTP client.
#
# Here is an example of a responder command that make usage of this response
# field. It basically returns to the T2WS web server a file name and the
# instruction to run the template engine. Since the T2WS server may not know
# the specific file endings used by the template files it can be necessary to
# specify explicitly the content type (as in the example), or to declare the
# Mime type of the template file endings (see <t2ws::DefineMimeType>).
#
# > proc Responder_GetFile {Request} {
# > set File [dict get $Request URITail]
# > return [dict create File $File IsTemplate 1 Content-Type .html]
# > }
#
# Template file syntax:
#
# The template files or the unprocessed raw data have to contain pre-written
# markup (e.g. HTML) together with Tcl code that is evaluated and replaced by
# the template engine. This Tcl code can inserted in 2 manners into the
# template files/raw data :
#
# * Inline Tcl code: Backslash, command and variable substitution is
# performed on each line.
# * Tcl control line: These lines are marked with the character '%' on the
# line start. They are usually used to add control
# constructs like conditions, loops, etc.
#
# The template is evaluated inside a procedure that is part of the 't2ws'
# namespace and that refers the 'Request' and 'Response' dictionary variables.
# The response dictionary can be directly modified via the 'dict' command, or
# via the commands <t2ws::SetResponse>, <t2ws::AddResponse> and
# <t2ws::UnsetResponse>.
#
# The following example contains inline Tcl code (command and variable) as well as
# Tcl control lines and it reads some fields of the 'Request' dictionary :
#
# > <!DOCTYPE html>
# > <html><body>
# > <h1>Web client:</h1>
# > %catch {
# > <p>Host: [dict get $Request Header host]</p>
# > <p>User-Agent: [dict get $Request Header user-agent]</p>
# > %}
# > <h1>Tcl Plattform:</h1>
# > <table class="t_table">
# > <thead>
# > <tr><th>Name</th><th>Data</th></tr>
# > </thead>
# > <tbody>
# > %foreach {Name Value} [array get tcl_platform] {
# > <tr><td>$Name</td><td>$Value</td></tr>
# > %}
# > </tbody>
# > </table>
# > </body></html>
# Package requirements, configurations and variables
package require Tcl 8.5
package require t2ws
namespace eval t2ws::template {}
# Register a T2WS plugin that handles invokes the template engine if a response
# defines the IsTemplate field as true (e.g. 1).
proc t2ws::template::PostPluginCmd {} {
upvar Request Request
upvar Response Response
# Don't run the template engine if an error happened
if {[dict exists $Response ErrorStatus]} return
# Don't run the template engine if the IsTemplate field is not set to true
if {![dict exists $Response IsTemplate] || ![dict get $Response IsTemplate]} return
# Run the template engine
set Script [GetTemplateScript [dict get $Response Body] TemplateEvalResult]
eval $Script
# Save the processed data in the response body
dict set Response Body $TemplateEvalResult
}
# Register the plugin
t2ws::DefinePlugin t2ws::template Post t2ws::template::PostPluginCmd
# Group: Template commands
# This plugin provides additional template commands that allow evaluating
# templates in a more explicit manner. <t2ws::template::GetTemplateScript> translates a
# template into a script that can then be evaluated in a custom procedure. And
# <t2ws::template::ProcessTemplate> and <t2ws::template::ProcessTemplateFile> evaluate respectively
# template raw data and template files. Note that templates evaluated by these
# commands cannot access the 'Request' and 'Response' dictionary variables!
##########################
# Proc: t2ws::template::GetTemplateScript
# This command translates a template into a script that will generate the
# processed template file if it is executed.
#
# Parameters:
# [Template] - Template data (e.g. HTML)
# [EvalVar] - Temporary variable to use during the template processing
#
# Returns:
# Template script
#
# Examples:
# > proc Responder_GetFile {Request} {
# > # Read the file content
# > set File [dict get $Request URITail]
# > set f [open $File]
# > set TemplateData [read $f]
# > close $f
# >
# > # Evaluate the template
# > set Script [GetTemplateScript $Template TemplateEvalData]
# > eval $Script
# >
# > # Return the evaluated data
# > return [dict create Body $TemplateEvalData Content-Type .html]
# > }
#
# See also:
# <t2ws::template::ProcessTemplateFile>
##########################
# Inspired by: http://wiki.tcl.tk/18455
proc t2ws::template::GetTemplateScript {Template {EvalVar ::t2ws::template::TemplateScript}} {
set Script "set $EvalVar \"\"\n"
foreach Line [split $Template "\n"] {
if {[string index $Line 0]=="%"} {
append Script [string range $Line 1 end] "\n"
} else {
append Script "append $EvalVar \[subst \{$Line\}\] \"\\n\"\n"
}
}
return $Script
}
##########################
# Proc: t2ws::template::ProcessTemplate
# This command processes a template provided in form of text and returns
# the generated data.
#
# Parameters:
# [Template] - Template data (e.g. HTML)
# [EvalVar] - Temporary variable to use during the template processing
#
# Returns:
# Evaluated template/generated data
#
# Examples:
# > proc Responder_GetFile {Request} {
# > # Read the file content
# > set File [dict get $Request URITail]
# > set f [open $File]
# > set TemplateData [read $f]
# > close $f
# >
# > # Evaluate the template
# > set TemplateEvalData [ProcessTemplate $TemplateData]
# >
# > # Return the evaluated data
# > return [dict create Body $TemplateEvalData Content-Type .html]
# > }
#
# See also:
# <t2ws::template::ProcessTemplateFile>
##########################
proc t2ws::template::ProcessTemplate {Template {EvalVar ::t2ws::template::TemplateScript}} {
uplevel #0 [GetTemplateScript $Template $EvalVar]
set ProcessTemplate [set $EvalVar]
unset $EvalVar
return $ProcessTemplate
}
##########################
# Proc: t2ws::template::ProcessTemplateFile
# This command processes a template provided in form of a file and
# returns the generated data.
#
# Parameters:
# [TemplateFile] - Template file (e.g. HTML)
#
# Returns:
# Evaluated template/generated data
#
# Examples:
# > proc Responder_GetFile {Request} {
# > set File [dict get $Request URITail]
# > switch [file extension $File] {
# > .htmt {
# > return [dict create Body [t2ws::template::ProcessTemplateFile $File] Content-Type .html]}
# > default {
# > return [dict create File $File]}
# > }
# > }
#
# See also:
# <t2ws::template::ProcessTemplate>
##########################
proc t2ws::template::ProcessTemplateFile {TemplateFile} {
set f [open $TemplateFile]
set Template [read $f]
close $f
return [ProcessTemplate $Template]
}
# Specify the t2ws version that is provided by this file:
package provide t2ws::template 0.4
##################################################
# Modifications:
# $Log: $
##################################################