Awesome Open Source
Awesome Open Source

======== ninjadog

.. image:: :target:

.. image:: :target:

.. image:: :target: :alt: Updates

.. image::

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

.. code-block:: python



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

It allows you to take something like this

.. code-block:: pug

        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

.. code-block:: python

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

.. code-block:: html

<!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.

.. code-block:: python

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


.. code-block:: html

<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.


.. _pug: .. _jade: .. _pyjade: .. _pypugjs: .. _pug-cli: .. _pug cli: .. _jinja2: .. _jinja 2: .. _pyramid:

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (54,340
html (11,138
pug (97
jinja2 (40