-
Notifications
You must be signed in to change notification settings - Fork 2
/
toolatra_templates.tcl
81 lines (76 loc) · 2.05 KB
/
toolatra_templates.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
#!/usr/bin/env tclsh
# Toolatra - Sinatra-like web microframework for Tcl 8.5/8.6
# Copyright (C) Tim K/RoverAMD 2018-2019 <timprogrammer@rambler.ru>.
#
# File: toolatra_templates.tcl
# Description: Addon for Toolatra that allows rendering templates
# License: MIT License
proc _toolatra_varpingpong {vr} {
return $vr
}
proc _toolatra_template_load {relPath {context -1}} {
if {$context != -1} {
dict for {key val} $context {
set $key $val
}
}
foreach key [info globals] {
global $key
}
set desc [open $relPath r]
set contents [read $desc]
close $desc
set result ""
set tmpEval ""
set insideEval 0
for {set index 0} {$index < [string length $contents]} {incr index} {
set cchar [string index $contents $index]
if {$cchar == "@"} {
set cchar2 [string index $contents [expr $index+1]]
if {$cchar2=="@"} {
incr index ;
set result "$result@" ;
continue ;
}
if {$insideEval} {
set insideEval 0
if {[info exists $tmpEval]} {
set result "$result[eval "_toolatra_varpingpong \$$tmpEval"]"
} elseif {[string index $tmpEval 0] == {!}} {
set substrTmpEval [string trim [string range $tmpEval 1 end]]
set result "$result[layout $substrTmpEval $context]"
} else {
set result "$result[eval $tmpEval]"
}
set tmpEval ""
} else {
set insideEval 1
set tmpEval ""
}
} elseif {$insideEval} {
set tmpEval "$tmpEval$cchar"
} else {
set result "$result$cchar"
}
}
return $result
}
proc layout {name {cntx -1}} {
set layoutsDir "[pwd]/layouts"
set lt "$layoutsDir/$name"
if {! [file exists $lt]} {
return "No such file or directory - \"$lt\" (layout: $name)."
} else {
return [_toolatra_template_load $lt $cntx]
}
}
proc etcl {name {cntx -1}} {
set relPath [pwd]/templates/$name
if {! [file exists $relPath]} {
error "No such file or directory - \"$relPath\"."
}
show [_toolatra_template_load $relPath $cntx]
}
package provide ToolatraTemplates 19.11
package require Toolatra 19.10
package require Tcl 8.5