A Nim-based HTML templating engine inspired by Python's Jinja.
Performs Compile-time template validation, to ensure you don't get any nasty surprises when running your code.
Global Installation:
nimble install templater
Local Installation:
atlas init
atlas use templater
import templater
const myTemplate = """
<!DOCTYPE html>
<html>
<head>
<title>{{pageTitle}}</title>
</head>
<body>
<h1>{{myHeader}}</h1>
<ul>{{#for item in items | index}}
<li>{{#index}}: {{#item}}</li>
{{#endfor}}</ul>
</body>
</html>
"""
let vars = newVarTable(("pageTitle", newVariable("My Page Title")), ("myHeader", newVariable("Page Header")), ("items", newVariable(@[newVariable("Item 0"), newVariable("Item 1")])))
let renderedTemplate = loadTemplate(myTemplate, vars)
let vars = newVarTable(("pageTitle", newVariable("My Page Title")), ("myHeader", newVariable("Page Header")), ("items", newVariable(@[newVariable("Item 0"), newVariable("Item 1")])))
let renderedTemplate = loadTemplateFile(staticRead("template.html"), vars)
Variables:
<!DOCTYPE html>
<html>
<head>
<title>{{pageTitle}}</title>
</head>
<body>
<h1>{{welcomeMessage}}</h1>
</body>
</html>
Lists:
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<li>
{{#for item in items}}
<li>{{#item}}</li>
{{#endfor}}
</li>
<li>
{{#for item in items | index}} <!--index here is optional, and can be named anything-->
<li>{{#index}}: {{#item}}</li>
{{#endfor}}
</li>
</body>
</html>