Skip to content

Wraith29/templater

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Docs

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>

About

A HTML Templating Engine written for Nim

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages