1- import ../ src / tim
1+ # This is an example of using Tim Engine with HttpBeast
22import 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
1121proc 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
1727proc 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!"
0 commit comments