Skip to content

Commit

Permalink
example using Tim from Node/Bun JavaScript
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Mar 22, 2024
1 parent e52cbad commit 48d0942
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
77 changes: 77 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const path = require('path')
const http = require('http')
const tim = require('../bin/tim.node')

// Tim Engine - Initialize the filesystem
tim.init(
"templates",
"storage",
path.resolve(__dirname), false, 2
)

// Tim Engine - Precompile available templates
// exposing some basic data to the global storage
let now = new Date()
tim.precompile({
year: now.getFullYear(),
stylesheets: [
{
type: "stylesheet",
src: "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
},
{
type: "preconnect",
src: "https://fonts.googleapis.com"
},
{
type: "preconnect",
src: "https://fonts.gstatic.com"
},
{
type: "stylesheet",
src: "https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap"
}
]
})

// Let's create a simple server using `std/http` module
const
host = 'localhost'
port = '3000'

http.createServer(
function(req, res) {
res.setHeader('Content-Type', 'text/html')
res.setHeader('charset', 'utf-8')
if(req.url == '/') {
res.writeHead(200)
res.end(
tim.render("index", "base", {
meta: {
title: "Tim Engine is Awesome!"
}
})
)
} else if(req.url == '/about') {
res.writeHead(200)
res.end(
tim.render("about", "secondary", {
meta: {
title: "Tim Engine is Awesome!"
}
})
)
} else {
res.writeHead(404)
res.end(
tim.render("error", "base", {
meta: {
title: "Oh, you're a genius!",
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!"
}
})
)
}
}).listen(port, host, () => {
console.log(`Server is running on http://${host}:${port}`)
})
17 changes: 9 additions & 8 deletions src/tim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ template layoutWrapper(getViewBlock) {.dirty.} =
add result, layoutTail

proc render*(engine: TimEngine, viewName: string,
layoutName = defaultLayout, global, local = newJObject()): string =
layoutName = defaultLayout, local = newJObject()): string =
## Renders a view based on `viewName` and `layoutName`.
## Exposing data to a template is possible using `global` or
## `local` objects.
Expand Down Expand Up @@ -386,7 +386,7 @@ proc render*(engine: TimEngine, viewName: string,

when defined napibuild:
# Setup for building TimEngine as a node addon via NAPI
import pkg/denim
import pkg/[denim, jsony]
from std/sequtils import toSeq

var timjs: TimEngine
Expand All @@ -402,17 +402,18 @@ when defined napibuild:
args.get("indent").getInt
)

proc precompile() {.export_napi.} =
proc precompile(globals: object) {.export_napi.} =
## Precompile TimEngine templates
timjs.precompile(flush = true, waitThread = false)
var globals: JsonNode = jsony.fromJson($(args.get("globals")))
timjs.precompile(flush = true, global = globals, waitThread = false)

proc render(view: string, layout: string = "base") {.export_napi.} =
proc render(view: string, layout: string, local: object) {.export_napi.} =
## Render a `view` by name
var layoutName = args.get("layout").getStr
echo layoutName
var local: JsonNode = jsony.fromJson($(args.get("local")))
let x = timjs.render(
args.get("view").getStr,
args.get("layout").getStr
args.get("layout").getStr,
local
)
return %*(x)

Expand Down

0 comments on commit 48d0942

Please sign in to comment.