Created
April 23, 2018 20:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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