GithubHelp home page GithubHelp logo

sirex / jinjatag Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mankyd/jinjatag

1.0 1.0 1.0 135 KB

jinjatag makes writing jinja2 extensions trivial. Decorate a function and get an extension.

License: Other

Python 100.00%

jinjatag's Introduction

A Library to Quickly Turn Functions into Jinja2 Extensions

This library is best demonstrated through example:

import jinjatag

### Configure your Jinja2 Environment:
jinja_tag = jinjatag.JinjaTag()
jinja2_env = Environment(extensions=[jinja_tag])
jinja_tag.init()

### Decorate your functions

@jinjatag.simple_tag()
def mytag(foo, bar=None, **kwargs):
    return "mytag: "+foo+(bar or '')+str(kwargs)

@jinjatag.simple_block()
def myblock(body, foo, bar=None):
    return "myblock : "+body+foo+(bar or '')

Then, in your template code, use your new tags:

{% mytag foo='abc' xyz='def' %}

{% myblock foo='bar' %}contents{% endmyblock %}

The resulting output:

mytag: abc{'xyz':'def'}

myblock: contentsbar

That's really all there is to it. A simple_tag is expected to accept 0 or more arguments and return a string. A simple_block is expected to accept at least one argument which will be its rendered body.

Install

The module is registered with pypi, so simply use:

easy_install jinjatag

or, if you're so inclined:

pip install jinjatag

Using with Flask

To use jinjatag with flask you have to configure it like this:

import jinjatag

app = Flask(__name__)

jinja_tag = jinjatag.JinjaTag()
# jinja_options is an ImmutableDict, so we have to do this song and dance
app.jinja_options = app.jinja_options.copy()
app.jinja_options['extensions'].append(jinja_tag)
jinja_tag.env = app.jinja_env
jinja_tag.init() 

Further Documentation

More documentation is available at http://mankyd.github.com/jinjatag/

Also, tests are available in https://github.com/mankyd/jinjatag/tree/master/jinjatag/tests

License

GPL v3

Authors

Dave Mankoff Mike Axiak

jinjatag's People

Contributors

axiak avatar hdknr avatar mankyd avatar mkoryak avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

ali1rathore

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.