Skip to content

Commit

Permalink
fixed rendering error if only a filepath argument is given to renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
knowsuchagency committed Jun 23, 2017
1 parent 4fc80b8 commit 1410c94
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
73 changes: 69 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,75 @@ 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!

It allows you to take something like this

.. code-block:: pug
html
head
title my jade 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
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(filepath=file, context=context, pretty=True))
to render this

.. code-block:: html

<!DOCTYPE html>
<html>
<head>
<title>my jade 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 and pug syntax for unparalleled
template-rendering power.

.. code-block:: python
from ninjadog import jinja2_renderer
Expand All @@ -77,7 +144,6 @@ within pug templates for even greater power!
context=context,
pretty=True))
This will render
.. code-block:: html

Expand All @@ -97,9 +163,8 @@ Only that which can be serialized to json will be passed to the
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.
`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,
Expand Down
6 changes: 5 additions & 1 deletion ninjadog/ninjadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def render(string: str = '',
# return render of string
if filepath and not string:
with open(filepath) as fp:
return render(fp.read(), filepath)
return render(fp.read(),
filepath,
context=context,
pretty=pretty,
pug_cli_path=pug_cli_path)

with NamedTemporaryFile('w') as fp:
fp.write(string)
Expand Down

0 comments on commit 1410c94

Please sign in to comment.