Skip to main content

Pug template support in Python

Project description

https://img.shields.io/pypi/v/ninjadog.svg https://img.shields.io/travis/knowsuchagency/ninjadog.svg Documentation Status Updates

Pug template (formerly jade) support in Python

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ninjadog-0.1.0.tar.gz (21.4 kB view hashes)

Uploaded Source

Built Distribution

ninjadog-0.1.0-py2.py3-none-any.whl (10.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page