All Projects → knowsuchagency → Ninjadog

knowsuchagency / Ninjadog

Licence: mit
Pug/Jinja template support for Python.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Ninjadog

Blockchain Demo
A web-based demonstration of blockchain concepts.
Stars: ✭ 4,074 (+20270%)
Mutual labels:  pug
Bemto
Smart mixins for writing BEM in Pug
Stars: ✭ 429 (+2045%)
Mutual labels:  pug
Nth Start Project
Startkit for HTML / CSS / JS pages layout.
Stars: ✭ 578 (+2790%)
Mutual labels:  pug
Sorrypy
sorry的python重制版
Stars: ✭ 335 (+1575%)
Mutual labels:  jinja2
Automatron
Infrastructure monitoring framework turning DevOps runbooks into automated actions
Stars: ✭ 381 (+1805%)
Mutual labels:  jinja2
Uikit Ecommerce Template
E-commerce template built with UIKIt
Stars: ✭ 453 (+2165%)
Mutual labels:  pug
Jinja2 Cli
CLI for Jinja2
Stars: ✭ 302 (+1410%)
Mutual labels:  jinja2
Dotdrop
Save your dotfiles once, deploy them everywhere
Stars: ✭ 813 (+3965%)
Mutual labels:  jinja2
Pingy Cli
The Simple Frontend Build Tool. No Configuration, No Plugins.
Stars: ✭ 390 (+1850%)
Mutual labels:  pug
Node Express Mongoose Demo
A simple demo app using node and mongodb for beginners
Stars: ✭ 4,976 (+24780%)
Mutual labels:  pug
Pug
Pug template engine for PHP
Stars: ✭ 341 (+1605%)
Mutual labels:  pug
Grow
A declarative website generator designed for high-quality websites, with a focus on easy maintenance and localization.
Stars: ✭ 360 (+1700%)
Mutual labels:  jinja2
Laravel Vue Boilerplate
🐘 A Laravel 6 SPA boilerplate with a users CRUD using Vue.js 2.6, GraphQL, Bootstrap 4, TypeScript, Sass, and Pug.
Stars: ✭ 472 (+2260%)
Mutual labels:  pug
Pug Starter
Simple pug (jade) starter [framework] enabling faster delivery of HTML & CSS projects to a private server and/or automatic deployment of GitHub pages.
Stars: ✭ 328 (+1540%)
Mutual labels:  pug
Bootstrap Switch
Turn checkboxes and radio buttons in toggle switches.
Stars: ✭ 5,132 (+25560%)
Mutual labels:  pug
Kratos Boilerplate
🔥 A simple boilerplate for creating statics PWA using Webpack, Pug, PostCSS and CSS Modules
Stars: ✭ 308 (+1440%)
Mutual labels:  pug
Bootstrap Flask
Bootstrap 4 helper for Flask/Jinja2.
Stars: ✭ 441 (+2105%)
Mutual labels:  jinja2
Gmdjs
Grid Material Design
Stars: ✭ 24 (+20%)
Mutual labels:  pug
Generator Angular Fullstack
Yeoman generator for an Angular app with an Express server
Stars: ✭ 6,135 (+30575%)
Mutual labels:  pug
Gulp Pug
Gulp plugin for compiling Pug templates
Stars: ✭ 512 (+2460%)
Mutual labels:  pug

======== ninjadog

.. image:: https://img.shields.io/pypi/v/ninjadog.svg :target: https://pypi.org/project/ninjadog/

.. image:: https://img.shields.io/travis/knowsuchagency/ninjadog.svg :target: https://travis-ci.org/knowsuchagency/ninjadog

.. image:: https://pyup.io/repos/github/knowsuchagency/ninjadog/shield.svg :target: https://pyup.io/repos/github/knowsuchagency/ninjadog/ :alt: Updates

.. image:: https://img.shields.io/github/license/mashape/apistatus.svg

Pug_ template (formerly jade_) support in Python

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

.. code-block:: python

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

.. code-block:: pug

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

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

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

print(render(template_string,
             context=context,
             pretty=True,
             with_jinja=True))

.. code-block:: html

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

  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: https://pugjs.org/api/getting-started.html .. _jade: https://naltatis.github.io/jade-syntax-docs/ .. _pyjade: https://github.com/syrusakbary/pyjade .. _pypugjs: https://github.com/matannoam/pypugjs .. _pug-cli: https://www.npmjs.com/package/pug-cli .. _pug cli: https://www.npmjs.com/package/pug-cli .. _jinja2: http://jinja.pocoo.org/ .. _jinja 2: http://jinja.pocoo.org/ .. _pyramid: https://trypyramid.com/

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].