Views & Templating
We are living in a single page world
Although glim has a fully featured view layer to support server side templates, currently there exists single page fashion which allows client side templating. Therefore, although this is completely optional, it is highly suggested not to use server side view rendering for keeping the server only busy with data retrieval. Instead, use some great javascript frameworks like angular.js
Jinja2 extension for glim framework
After 0.9 releases of glim, view layer is separated from glim core and served as an extension. Therefore, it is required to install glim-extensions module using pip
pip install glim-extensions
Configuration
# app/config/<env>.py
config = {
'extensions': {
'view': {
'package': 'app.views'
},
}
}
There exists a really simple jinja2 integration in glim. The view layer in glim simply is a set of jinja2 templates. Glim views can be defined in app/views folder. The right place to render templates would be inside controllers. So, letβs make a controller return a jinja2 template;
# controllers.py
from glim_extensions.view import View
from glim import Controller
from app.models import Post
from glim_extensions.db import Orm
class PostController(Controller):
def get(id):
p = Orm.query(Post.post_id, Post.content).filter_by(post_id=id).first()
if p:
post = dict(zip(post.keys(), p))
return View.render('post', post=post)
else:
return Response(status=404)
The template file would be the following;
# views/post.html
<html>
<body>
Post id : {{ post.id }}
Post content : {{ post.content }}
</body>
</html>
Logic in templates
Looping
If - Else Statements
Including templates
Extending templates
Sharing data between templates
Configuring template path
Updated less than a minute ago