forked from kirpit/web2jinja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb2jinja.py
51 lines (40 loc) · 1.4 KB
/
web2jinja.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
"""
<web2jinja.py>
https://github.com/kirpit/web2jinja
web2py controller decorator that returns Jinja2 compiled template string.
Example usage:
Place this file under your web2py applications/myapp/modules.
```python
# myapp/controllers/somecontroller.py
from applications.myapp.modules.web2jinja import Web2Jinja
@Web2Jinja(request)
def index():
return {
'foo': 'bar',
}
```
will render same view file i.e. applications/myapp/views/somecontroller/index.html
by using Jinja2!
==================================================
The MIT License (MIT)
Copyright (c) <2012> <Roy Enjoy a.k.a. kirpit>
http://www.opensource.org/licenses/mit-license.php
==================================================
"""
from jinja2 import Environment, PackageLoader
class Web2Jinja(object):
template = None
func = None
def __init__(self, request):
app = 'applications.%s' % request.application
jinja_env = Environment(loader=PackageLoader(app, 'views'))
template_name = '%s/%s.%s' % (request.controller,
request.function,
request.extension)
self.template = jinja_env.get_template(template_name)
def __call__(self, func):
def wrapper(*args, **kwargs):
context = func(*args, **kwargs)
return self.template.render(**context)
return wrapper