You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alice Zoë Bevan–McGregor edited this page Feb 7, 2020
·
4 revisions
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:
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
fromcinjeimportflatten# cinje can write out the result for youfromtemplateimporthello# load up your templatecinje.flatten(hello(), file='hello.html', encoding='utf-8')
This can even be incorporated into the template itself as an if-main block:
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.