|
1 | | -#This example demonstrates using Tim with Prologue |
2 | | -import std/[strutils, times] |
3 | | -import prologue |
4 | | -include ./initializer |
| 1 | +# This is an example of using Tim Engine with Prologue |
| 2 | + |
| 3 | +import std/[strutils, times, os] |
| 4 | +import ../src/tim |
| 5 | + |
| 6 | +# |
| 7 | +# Setup Tim Engine |
| 8 | +# |
| 9 | +var |
| 10 | + timEngine = newTim( |
| 11 | + src = "templates", # where to find the .timl files |
| 12 | + output = "storage", # where to cache precompiled templates |
| 13 | + basepath = getCurrentDir() # base path for resolving absolute paths in templates |
| 14 | + ) |
5 | 15 |
|
6 | | -#init Settings for prologue |
7 | | -let |
8 | | - settings = newSettings(port = Port(8082)) |
| 16 | +timEngine.precompile() |
| 17 | + |
| 18 | +# |
| 19 | +# Setup Prologue |
| 20 | +# |
| 21 | +import prologue |
9 | 22 |
|
| 23 | +let settings = newSettings(port = Port(8082)) |
10 | 24 | var app = newApp(settings = settings) |
11 | 25 |
|
12 | 26 | #define your route handling callbacks |
13 | 27 | proc indexPageHandler(ctx: Context) {.async, gcsafe.} = |
| 28 | + {.gcsafe.}: |
14 | 29 | let localObjects = %*{ |
15 | | - "meta": { |
16 | | - "title": "Tim Engine is Awesome!" |
17 | | - }, |
18 | | - "path": "/" |
| 30 | + "meta": { |
| 31 | + "title": "Tim Engine is Awesome!" |
| 32 | + }, |
| 33 | + "path": "/" |
19 | 34 | } |
20 | | - |
21 | | - {.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe |
22 | | - let indexPage = timl.render(viewName = "index", layoutName = "base", local = localObjects) |
23 | | - |
| 35 | + let indexPage = timEngine.render(view = "index", data = localObjects) |
24 | 36 | resp indexPage |
25 | 37 |
|
26 | 38 | proc aboutPageHandler(ctx: Context) {.async, gcsafe.} = |
| 39 | + {.gcsafe.}: |
27 | 40 | let localObjects = %*{ |
28 | | - "meta": { |
29 | | - "title": "About Tim Engine" |
30 | | - }, |
31 | | - "path": "/about" |
| 41 | + "meta": { |
| 42 | + "title": "About Tim Engine" |
| 43 | + }, |
| 44 | + "path": "/about" |
32 | 45 | } |
33 | | - {.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe |
34 | | - let aboutPage = timl.render(viewName = "about", layoutName = "secondary", local = localObjects) |
35 | | - |
| 46 | + let aboutPage = timEngine.render(view = "about", layout = "secondary", data = localObjects) |
36 | 47 | resp aboutPage |
37 | 48 |
|
38 | 49 | proc e404(ctx: Context) {.async, gcsafe.} = |
| 50 | + {.gcsafe.}: |
39 | 51 | let localObjects = %*{ |
40 | | - "meta": { |
41 | | - "title": "Oh, you're a genius!", |
42 | | - "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!" |
43 | | - }, |
44 | | - "path": %*(ctx.request.path) |
| 52 | + "meta": { |
| 53 | + "title": "Oh, you're a genius!", |
| 54 | + "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!" |
| 55 | + }, |
| 56 | + "path": %*(ctx.request.path) |
45 | 57 | } |
46 | | - {.cast(gcsafe).}: #timl is a global using GC'ed memory and prologue loves it's callbacks to be gc-safe |
47 | | - let e404Page = timl.render(viewName = "error", layoutName = "base", local = localObjects) |
48 | | - |
| 58 | + let e404Page = timEngine.render(view = "error", data = localObjects) |
49 | 59 | resp e404Page |
50 | 60 |
|
51 | 61 | #tell prologue how to handle routes |
|
0 commit comments