mirror of
https://github.com/Wraith29/templater
synced 2026-01-09 02:01:05 +00:00
No description
| src | ||
| tests | ||
| .gitignore | ||
| atlas.workspace | ||
| LICENSE.txt | ||
| nim.cfg | ||
| README.md | ||
| templater.nimble | ||
| TODO.md | ||
Templater
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.
Installation
Global Installation:
nimble install templater
Local Installation:
atlas init
atlas use templater
Usage
Load from Template String
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)
Load from a Template file
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)
Template Syntax
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>