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 Updates

Pug template (formerly jade) support in Python

Installation

ninjadog requires Python 3.6, node-js, npm, and the pug-cli library

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 leverages the pug-cli library, written in nodejs, to render pug templates in Python. In addition, you can utilize jinja2 syntax within pug templates for even greater power!

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 {{ "Don't" 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>Don't stop believing</p>

How?

Jinja2 basically behaves as a preprocessor to the pug template engine. All data passed as the context will be processed by jinja2. Only that which can be serialized to json will be passed to the pug cli.

Why?

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 have some bugs and the maintenance is a bit lacking IMO.

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. This means that it can’t be as fast as a native template engine like pyjade, but it will be more reliable as it’s leveraging the popular and well-maintained nodejs implementation.

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.1.tar.gz (103.4 kB view hashes)

Uploaded Source

Built Distribution

ninjadog-0.1.1-py2.py3-none-any.whl (10.1 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