Skip to content

Commit

Permalink
adds jinja2 filters support (#41)
Browse files Browse the repository at this point in the history
* adds jinja2 filters support

* removes redundant filter value check
  • Loading branch information
corrado-s authored and asvetlov committed Nov 24, 2016
1 parent ac1efbb commit 9d7b21a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aiohttp_jinja2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
APP_CONTEXT_PROCESSORS_KEY = 'aiohttp_jinja2_context_processors'


def setup(app, *args, app_key=APP_KEY, context_processors=(), **kwargs):
def setup(app, *args, app_key=APP_KEY, context_processors=(), filters=None, **kwargs):
env = jinja2.Environment(*args, **kwargs)
if filters is not None:
env.filters.update(filters)
app[app_key] = env
if context_processors:
app[APP_CONTEXT_PROCESSORS_KEY] = context_processors
Expand Down
31 changes: 31 additions & 0 deletions tests/test_jinja_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import aiohttp
import aiohttp_jinja2
import asyncio
import jinja2
import pytest


@pytest.mark.run_loop
def test_jinja_filters(create_server, loop):

@aiohttp_jinja2.template('tmpl.jinja2')
@asyncio.coroutine
def index(request):
return {}

def add_2(value):
return value + 2

app, url = yield from create_server()
aiohttp_jinja2.setup(
app,
loader=jinja2.DictLoader({'tmpl.jinja2': "{{ 5|add_2 }}"}),
filters={'add_2': add_2}
)

app.router.add_route('GET', '/', index)

resp = yield from aiohttp.request('GET', url, loop=loop)
assert 200 == resp.status
txt = yield from resp.text()
assert '7' == txt

0 comments on commit 9d7b21a

Please sign in to comment.