Pug template support in Python
Project description
Pug template (formerly jade) support in Python
Free software: MIT license
Documentation: http://journalpanic.com/ninjadog
Installation
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
config.include('ninjadog')
Usage
ninjadog leverages the pug-cli library, written in nodejs, to render pug templates in Python.
It allows you to take something like this
html
head
title my pug template
body
#content
h1 Hello #{name}
.block
input#bar.foo1.foo2
input(type="text", placeholder="your name")
if name == "Bob"
h2 Hello Bob
ul
for book in books
li= book
else
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(filepath=file, context=context, pretty=True))
to render this
<!DOCTYPE html>
<html>
<head>
<title>my pug template</title>
</head>
<body>
<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>
<ul>
<li>coloring book</li>
<li>audio book</li>
<li>O'Reilly book</li>
</ul>
</div>
</div>
</body>
</html>
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
"""
print(render(template_string,
context=context,
pretty=True,
with_jinja=True))
<h1>hello, world</h1>
<p>it's time to celebrate!</p>
<p>Don't stop believing</p>
Why?
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 bugs and the maintenance is a bit lacking.
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
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.3.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b00eafebb997eb1cef35a9316e3b558eef9168ef2f94cf2e045a6729dce9094 |
|
MD5 | 69bb3ff35bdbcc8dab8f3dca084a42ee |
|
BLAKE2b-256 | fd06f93c352822c66e3671b3d73ce27b1bd060f50ae9d4d5dfef4d9943a7f630 |