Skip to content

Instantly share code, notes, and snippets.

@benman1
Created April 23, 2018 20:33
Show Gist options
  • Save benman1/8ce70fa056e68fc25ee3a12765949ecb to your computer and use it in GitHub Desktop.
Save benman1/8ce70fa056e68fc25ee3a12765949ecb to your computer and use it in GitHub Desktop.
Simple templating solution with key-value substitutions in a template - nim templating for bokeh html
import streams, nre
#[ This script reads a template file and substitutes according to
key-value pairs. All variables in the template start with $. For
example $elementid.
]#
type
Substitutions = seq[tuple[name: string, value: string]]
proc testPage (values: Substitutions): string =
var
fs = newFileStream("template.html", fmRead)
html_temp = fs.readAll()
for k, v in items(values):
var regex = re("\\$" & k)
html_temp = html_temp.replace(regex, v)
return html_temp
var
substs: Substitutions = @[(name: "elementid", value: "1099"), (name: "title", value: "MyBokehPlot"), (name: "x", value: "[0, 1, 2]"), (name: "y", value: "[3, 2, 1]")]
echo testPage(substs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment