-
Notifications
You must be signed in to change notification settings - Fork 1
/
web2jinja.py
63 lines (51 loc) · 1.94 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
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""
<web2jinja.py>
https://github.com/kirpit/web2jinja
web2jinja
=========
web2py controller decorator that returns Jinja2 compiled template string.
Usage
=====
Place this file under your web2py applications/myapp/modules.
```python
# myapp/controllers/somecontroller.py
from applications.myapp.modules.web2jinja import Web2Jinja
@Web2Jinja(locals())
def index():
return {
'foo': 'bar',
}
```
will render same view file i.e. applications/myapp/views/somecontroller/index.html <br>
by using Jinja2! It will include all the global variables such as request, <br>
response, session, cache and other helpers but only "ON" boolean helper will be missing.
License
=======
The MIT License (MIT)<br>
Copyright (c) 2012 Roy Enjoy a.k.a. kirpit<br>
http://www.opensource.org/licenses/mit-license.php
"""
from jinja2 import Environment, PackageLoader
class Web2Jinja(object):
web2py_locals = None
template = None
def __init__(self, web2py_locals):
# filter web2py globals
self.web2py_locals = dict([(k, v) for k, v in web2py_locals.iteritems()
if not k.startswith('_') and
getattr(v, '__module__', '').startswith('gluon')])
# create jinja2 env and template
request = self.web2py_locals['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):
# create template context
context = dict(func(*args, **kwargs).items() + self.web2py_locals.items())
return self.template.render(**context)
return wrapper