-
Notifications
You must be signed in to change notification settings - Fork 0
Use as File Generator
Using cinje template functions as a method of generating static HTML files from dynamic input (as an offline processing step) is trivial. First, you need one or more template files, which are just .py
files containing cinje template instructions, like the following:
template.py
# encoding: cinje
: from cinje.std.html import page
: def hello
: using page "Hello World"
Hi there.
This defines a template module containing one template function, which renders a full HTML5 page with the title Hello World
and a <body>
containing the text Hi there.
Then, you need something similar to the following tiny script to load the template, optionally pass data to it, then save the result somewhere:
render.py
from cinje import flatten # cinje can write out the result for you
from template import hello # load up your template
cinje.flatten(hello(), file='hello.html', encoding='utf-8')
This can even be incorporated into the template itself as an if-main block:
appended to template.py
: end # using
: end # def
: from cinje import flatten
: if __name__ == '__main__'
: flatten(hello(), file='hello.html', encoding='utf-8')
That's it. Note, however, that self-rendering is constrained by the remaining need to import the cinje
module prior to import/execution if no pre-existing valid bytecode can be found. This can be alleviated by the use of .pth
trickery.