Code Monkey home page Code Monkey logo

aiohttp_jinja2's Introduction

aiohttp_jinja2

jinja2 template renderer for aiohttp.web.

Usage

Before template rendering you have to setup jinja2 environment first:

app = web.Application()
aiohttp_jinja2.setup(app,
    loader=jinja2.FileSystemLoader('/path/to/templates/folder'))

After that you may to use template engine in your web-handlers. The most convenient way is to decorate a web-handler.

Using the function based web handlers:

@aiohttp_jinja2.template('tmpl.jinja2')
def handler(request):
    return {'name': 'Andrew', 'surname': 'Svetlov'}

Or for `Class Based Views <https://aiohttp.readthedocs.org/en/stable/web.html#class-based-views>`:

class Handler(web.View):
    @aiohttp_jinja2.template('tmpl.jinja2')
    @asyncio.coroutine
    def get(self):
        return {'name': 'Andrew', 'surname': 'Svetlov'}

On handler call the aiohttp_jinja2.template decorator will pass returned dictionary {'name': 'Andrew', 'surname': 'Svetlov'} into template named tmpl.jinja2 for getting resulting HTML text.

If you need more complex processing (set response headers for example) you may call render_template function.

Using a function based web handler:

@asyncio.coroutine
def handler(request):
    context = {'name': 'Andrew', 'surname': 'Svetlov'}
    response = aiohttp_jinja2.render_template('tmpl.jinja2',
                                              request,
                                              context)
    response.headers['Content-Language'] = 'ru'
    return response

Or, again, a class based view:

class Handler(web.View):
    @asyncio.coroutine
    def get(self):
        context = {'name': 'Andrew', 'surname': 'Svetlov'}
        response = aiohttp_jinja2.render_template('tmpl.jinja2',
                                                  self.request,
                                                  context)
        response.headers['Content-Language'] = 'ru'
        return response

License

aiohttp_jinja2 is offered under the Apache 2 license.

aiohttp_jinja2's People

Contributors

alexander-travov avatar asvetlov avatar iho avatar imbolc avatar jettify avatar kxepal avatar ludovic-gasc avatar mattrasband avatar popravich avatar s-y avatar tark-hidden avatar

Watchers

 avatar  avatar  avatar

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.