ninjadog
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(file=filepath, 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 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.
Gotchas
Currently, rendering a template with jinja2 syntax goes through the following process:
Render elements on the initial template through jinja2
Pass the output to the pug-cli, gathering extensions and inclusions in the process
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.
对不起
Metadata
Release files for ninjadog 0.5.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ninjadog-0.5.2.tar.gz | 290.4 kB | Without provenance | Details for ninjadog-0.5.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ninjadog-0.5.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for ninjadog-0.5.2-py2.py3-none-any.whl |
Total release size: 305.5 kB
Release files / ninjadog-0.5.2.tar.gz
| Download URL | ninjadog-0.5.2.tar.gz |
|---|---|
| Size | 290.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7b28ef3a6bbb051c834577b3d78ea5efa3cd707d6150ca1ce04f096b3428bbf
|
|
BLAKE2b-256 checksum How to use checksums |
d3833cda47f54d28a53d4add4e7da631f0096ea41ff4d3ff8b26640114284f74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / ninjadog-0.5.2-py2.py3-none-any.whl
| Download URL | ninjadog-0.5.2-py2.py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c2013d2c956884da5eaaf9c8b1632bed268e18cf3b1ee57d8468a622a7076faf
|
|
BLAKE2b-256 checksum How to use checksums |
fd9f9f98e241790d7d0d90fc4a96fb06f15366bb2f582d236ad0eab70de30c00
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for ninjadog 0.5.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.