Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Last active September 2, 2018 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharycarter/b68aded56439c77ebdf6022a729c61e8 to your computer and use it in GitHub Desktop.
Save zacharycarter/b68aded56439c77ebdf6022a729c61e8 to your computer and use it in GitHub Desktop.
<div>
<h1>Hello, World!</h1>
<h2>It is 8:44:27 PM.</h2>
</div>
tagged_template_literal.js:908:3
<div>
<h1>Hello, World!</h1>
<h2>It is 8:44:28 PM.</h2>
</div>
tagged_template_literal.js:908:3
<div>
<h1>Hello, World!</h1>
<h2>It is 8:44:29 PM.</h2>
</div>
tagged_template_literal.js:908:3
<div>
<h1>Hello, World!</h1>
<h2>It is 8:44:30 PM.</h2>
</div>
Hello, World!
It is 8:44:27 PM.
import dom, jsconsole, litz, jsffi
proc jsBind(p: JsObject, e: JsObject): JsObject {. importcpp: "#.bind(#)" .}
proc `^`(s: cstring): cstring {.importcpp: "eval('`' + # + '`')".}
proc t() {.html_templ.} =
# the 'html_templ' pragma provides
# a dsl for writing HTML
`div`: # could use 'd:' instead of '`div`:' - div is a reserved keyword in Nim
h1: "Hello, World!"
h2: "It is ${new Date().toLocaleTimeString()}."
proc renderT(t: cstring): JsObject =
var this {.nodecl, importc.}: JsObject
console.log(^t)
this.innerHTML = ^t
return this
proc tick(r: JsObject) =
var
t {.global.} = newT()
temp = t.render()
discard to(r, proc(t: cstring): JsObject)(temp)
proc litzb(e: Element): JsObject =
result = jsBind(toJs(renderT), toJs(e))
discard window.setInterval(proc () = tick(litzb(document.body)), 1000)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
</head>
<body>
<!-- page content -->
<script src="./nimcache/tagged_template_literal.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment