Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Pug template support in Python

Project description Updates

Pug template (formerly jade) support in Python


ninjadog requires Python 3, 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



ninjadog leverages the pug-cli library, written in nodejs, to render pug templates in Python.

It allows you to take something like this

        title my pug template
            h1 Hello #{name}
                input(type="text", placeholder="your name")
                if name == "Bob"
                    h2 Hello Bob
                    for book in books
                        li= book
                        li sorry, no books

and sprinkle some Python over it

from ninjadog import render

context = {
    'name': 'Bob',
    'books': ['coloring book', 'audio book', "O'Reilly book"],
    'type': 'text',

print(render(file=filepath, context=context, pretty=True))

to render this

<!DOCTYPE html>
    <title>my pug template</title>
    <div id="content">
      <h1>Hello Bob</h1>
      <div class="block">
        <input class="foo1 foo2" id="bar">
        <input type="text" placeholder="your name">
        <h2>Hello Bob</h2>
          <li>coloring book</li>
          <li>audio book</li>
          <li>O'Reilly book</li>

You can even combine jinja2 syntax for unparalleled template-rendering power.

from ninjadog import render

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

<h1>hello, world</h1>
<p>it's time to celebrate!</p>
<p>Don't stop believing</p>


Pug templates are a super elegant and expressive way to write html, IMO.

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

I figured it would be good to have an alternative method to render pug templates that used the native javascript rendering engine.

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 likely be more reliable over time as it’s leveraging the popular and well-maintained nodejs implementation.


Currently, rendering a template with jinja2 syntax goes through the following process:

  1. Render elements on the initial template through jinja2
  2. Pass the output to the pug-cli, gathering extensions and inclusions in the process
  3. Render the output through jinja2 again, since the original template may have extended or included other templates that countained jinja2 syntax themselves.

What this means is that if you want to escape jinja2 syntax, you need to do it twice.

For example, to have a literal {{ escaping inception }} rendered, you’ll need to have {{ "{{ '{{ escaping inception }}' }}" }} in your template.


Project details

Download files

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

Files for ninjadog, version 0.5.2
Filename, size File type Python version Upload date Hashes
Filename, size ninjadog-0.5.2-py2.py3-none-any.whl (15.1 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size ninjadog-0.5.2.tar.gz (290.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page