Pug template support in Python
Project description
Pug template (formerly jade) support in Python
Free software: MIT license
Documentation: https://ninjadog.readthedocs.io.
Installation
brew install npm npm install -g pug-cli pip install ninjadog
For use with Pyramid, just add it to the configuration
config.include('pyramid_jinja2')
config.include('ninjadog.ext.pyramid')
What?
ninjadog lets you render pug templates and combine them with jinja2 syntax.
from ninjadog import jinja2_renderer
def stop_believing():
return False
context = {
'stop_believing': stop_believing,
'happy': {
'birthday': 'today',
}
}
template_string = """
h1 hello, world
if happy.birthday == today
p it's time to celebrate!
p {{ 'never' if not stop_believing() }} stop believing
"""
print(jinja2_renderer(template_string,
context=context,
pretty=True))
This will render
<h1>hello, world</h1>
<p>it's time to celebrate!</p>
<p>never stop believing</p>
How?
Jinja2 basically behaves as a preprocessor to the pug template engine and any data passed via the context argument that are able to be serialized into json will then be passed to the pug template engine for rendering as well.
Why?
I think pug templates are a very elegant and expressive way to write html. It makes something akin to an exercise in corporal mortification almost pleasant.
There exists a project, pyjade and a less-popular fork, pypugjs, that are pure-python implementations of the pug template engine, but they haven’t been very well-maintained and and the bugs don’t lend themselves to fixes by mere-mortals like myself.
It made more sense to me to use the existing nodejs implementation, and find a way to have it play nicely with Python.
ninjadog does this by spawning the pug cli as a subprocess and communicating with it that way. Furthermore, if you want to use jinja2 template syntax with your pug templates, any pug template that extends from another will need to have that template rendered through jinja2 first, and since we can’t overwrite the original template, that means creating a temporary directory and copies of all the relevant templates in that directory to be rendered prior to passing it to the pug cli process.
All of that is to say that ninjadog is rather slow, but I’m willing to accept pull-requests to make it faster or convenient caching mechanisms.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ninjadog-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9656bacc84aa0525fc2bf02cf70275ac7c4bb042384806a05abdc3da459548b1 |
|
MD5 | fb6a0e03be0dc84fc246cbf6d8032534 |
|
BLAKE2b-256 | ceb20be06144276a3214388fc48386339a15e4c44d99826ff668e5d4a47de280 |