Skip to content

Commit d6c570e

Browse files
committed
update examples
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent 5d312b7 commit d6c570e

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

example/example_httpbeast.nim

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
import ../src/tim
1+
# This is an example of using Tim Engine with HttpBeast
22
import std/[options, asyncdispatch, macros,
3-
os, strutils, times, json]
4-
import pkg/[httpbeast]
3+
os, strutils, times, json, httpcore, net]
4+
5+
import ../src/tim
6+
7+
#
8+
# Setup Tim Engine
9+
#
10+
var
11+
timEngine = newTim(
12+
src = "templates", # where to find the .timl files
13+
output = "storage", # where to cache precompiled templates
14+
basepath = getCurrentDir() # base path for resolving absolute paths in templates
15+
)
516

6-
from std/httpcore import HttpCode, Http200
7-
from std/net import Port
17+
timEngine.precompile()
818

9-
include ./initializer
19+
import pkg/[httpbeast]
1020

1121
proc resp(req: Request, view: string, layout = "base", code = Http200,
12-
headers = "Content-Type: text/html", local = newJObject()) =
13-
local["path"] = %*(req.path.get())
14-
let htmlOutput = timl.render(view, layout, local = local)
22+
headers = "Content-Type: text/html", data = newJObject()) =
23+
data["path"] = %*(req.path.get())
24+
let htmlOutput = timEngine.render(view, layout, data = data)
1525
req.send(code, htmlOutput, headers)
1626

1727
proc onRequest(req: Request): Future[void] =
@@ -22,20 +32,20 @@ proc onRequest(req: Request): Future[void] =
2232
case path
2333
of "/":
2434
req.resp("index",
25-
local = %*{
35+
data = %*{
2636
"meta": {
2737
"title": "Tim Engine is Awesome!"
2838
}
2939
})
3040
of "/about":
3141
req.resp("about", "secondary",
32-
local = %*{
42+
data = %*{
3343
"meta": {
3444
"title": "About Tim Engine"
3545
}
3646
})
3747
else:
38-
req.resp("error", code = Http404, local = %*{
48+
req.resp("error", code = Http404, data = %*{
3949
"meta": {
4050
"title": "Oh, you're a genius!",
4151
"msg": "Oh yes, yes. It's got action, it's got drama, it's got dance! Oh, it's going to be a hit hit hit!"

example/templates/views/about.timl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ div.container > div.row > div.col-lg-12
1111
div.d-grid.gap-4.d-md-flex.justify-content-md-start
1212
a href="https://tim.openpeeps.dev" class="btn btn-outline-light border-2 rounded-3 px-4"
1313
"Check documentation"
14-
a href="/" class="btn btn-primary border-2 rounded-3 px-4"
14+
a href="https://github.com/openpeeps/tim" class="btn btn-primary border-2 rounded-3 px-4"
1515
"Check Tim on GitHub"

example/templates/views/index.timl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ div.container > div.row > div.col-lg-12
1212
em
1313
h1.display-3.fw-bold: "This is Tim Engine"
1414
p.h3.fw-light.mb-4: "A high-performance template engine & markup language written in Nim with a focus on developer experience and productivity"
15-
a href="/" class="btn btn-primary border-2 rounded-3 px-4": "Check Tim on GitHub"
15+
a href="/" class="btn fw-bold btn-outline-info border-2 rounded-3 px-4": "More about Tim"
1616

1717
div.col-lg-7 > div.row.g-3
1818
div.container.masonry-columns

0 commit comments

Comments
 (0)