Skip to content

Commit

Permalink
set global data at pre-compilation level
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Feb 21, 2024
1 parent 4f81df0 commit f7c1cfd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/tim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ const

proc jitCompiler(engine: TimEngine, tpl: TimTemplate, data: JsonNode): HtmlCompiler =
## Compiles `tpl` AST at runtime
newCompiler(engine.readAst(tpl), tpl, engine.isMinified(), engine.getIndentSize(), data)
engine.newCompiler(engine.readAst(tpl), tpl, engine.isMinified, engine.getIndentSize, data)

proc displayErrors(l: Logger) =
for err in l.errors:
display(err)
display(l.filePath)

proc compileCode(engine: TimEngine, tpl: TimTemplate, refreshAst = false) =
proc compileCode(engine: TimEngine, tpl: TimTemplate,
refreshAst = false) =
# Compiles `tpl` TimTemplate to either `.html` or binary `.ast`
var tplView: TimTemplate
if tpl.getType == ttView:
Expand All @@ -42,7 +43,8 @@ proc compileCode(engine: TimEngine, tpl: TimTemplate, refreshAst = false) =
else:
# otherwise, compiles the generated AST and save
# a pre-compiled HTML version on disk
var c = newCompiler(p.getAst, tpl, engine.isMinified, engine.getIndentSize)
var c = engine.newCompiler(p.getAst, tpl, engine.isMinified,
engine.getIndentSize)
if likely(not c.hasError):
case tpl.getType:
of ttView:
Expand All @@ -57,6 +59,7 @@ proc compileCode(engine: TimEngine, tpl: TimTemplate, refreshAst = false) =

var sync: Thread[(Port, int)]
var lastModified, prevModified: Time

proc browserSync(x: (Port, int)) {.thread.} =
proc onRequest(req: Request) {.async.} =
if req.httpMethod == some(HttpGet):
Expand Down Expand Up @@ -85,7 +88,7 @@ proc browserSync(x: (Port, int)) {.thread.} =

proc precompile*(engine: TimEngine, callback: TimCallback = nil,
flush = true, waitThread = false, browserSyncPort = Port(6502),
browserSyncDelay = 550) =
browserSyncDelay = 550, global: JsonNode = newJObject()) =
## Precompiles available templates inside `layouts` and `views`
## directories to either static `.html` or binary `.ast`.
##
Expand All @@ -97,6 +100,7 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil,
## Enable filesystem monitor by compiling with `-d:timHotCode` flag.
## You can create a separate thread for precompiling templates (use `waitThread` to keep the thread alive)
if flush: engine.flush()
engine.setGlobalData(global)
when not defined release:
when defined timHotCode:
# Define callback procs for pkg/watchout
Expand Down Expand Up @@ -172,7 +176,6 @@ template layoutWrapper(getViewBlock) {.dirty.} =
getViewBlock
layoutTail = layout.getTail()
else:

var jitLayout = engine.jitCompiler(layout, data)
if likely(not jitLayout.hasError):
add result, jitLayout.getHead()
Expand All @@ -191,7 +194,7 @@ proc render*(engine: TimEngine, viewName: string,
var
view: TimTemplate = engine.getView(viewName)
data: JsonNode = newJObject()
data["global"] = global
# data["global"] = global # todo merge global data
data["local"] = local
if likely(engine.hasLayout(layoutName)):
var layout: TimTemplate = engine.getLayout(layoutName)
Expand Down
5 changes: 3 additions & 2 deletions src/tim/engine/compilers/html.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ../ast, ../logging, ./js

from std/xmltree import escape
from ../meta import TimEngine, TimTemplate, TimTemplateType,
getType, getSourcePath
getType, getSourcePath, getGlobalData

import ./tim # TimCompiler object

Expand Down Expand Up @@ -1059,10 +1059,11 @@ proc walkNodes(c: var HtmlCompiler, nodes: seq[Node],
#

when not defined timStandalone:
proc newCompiler*(ast: Ast, tpl: TimTemplate, minify = true,
proc newCompiler*(engine: TimEngine, ast: Ast, tpl: TimTemplate, minify = true,
indent = 2, data: JsonNode = newJObject()): HtmlCompiler =
## Create a new instance of `HtmlCompiler`
assert indent in [2, 4]
data["global"] = engine.getGlobalData()
result =
HtmlCompiler(
ast: ast,
Expand Down
6 changes: 6 additions & 0 deletions src/tim/engine/meta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ proc getPath*(engine: TimEngine, key: string, templateType: TimTemplateType): st
result &= ".timl"
result = normalizedPath(result) # normalize path for Windows

proc setGlobalData*(engine: TimEngine, data: JsonNode) =
engine.globals = data

proc getGlobalData*(engine: TimEngine): JsonNode =
engine.globals

proc hashid(path: string): string =
# Creates an MD5 hashed version of `path`
result = getMD5(path)
Expand Down

0 comments on commit f7c1cfd

Please sign in to comment.