{"_id":"553d394e9660ba0d00569d19","user":"543466ea0e8e2b0e00341818","category":{"_id":"553d394d9660ba0d00569d09","pages":["553d394e9660ba0d00569d0a","553d394e9660ba0d00569d0b","553d394e9660ba0d00569d0c","553d394e9660ba0d00569d0d","553d394e9660ba0d00569d0e","553d394e9660ba0d00569d0f","553d394e9660ba0d00569d10","553d394e9660ba0d00569d11","553d394e9660ba0d00569d12"],"project":"543467200ef9c00800164ecc","__v":1,"version":"553d394d9660ba0d00569d07","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-15T18:17:35.811Z","from_sync":false,"order":1,"slug":"extensions","title":"EXTENSIONS"},"project":"543467200ef9c00800164ecc","githubsync":"","version":{"_id":"553d394d9660ba0d00569d07","project":"543467200ef9c00800164ecc","forked_from":"54bedb2fffb79c320030db1f","__v":1,"createdAt":"2015-04-26T19:15:25.245Z","releaseDate":"2015-04-26T19:15:25.245Z","categories":["553d394d9660ba0d00569d08","553d394d9660ba0d00569d09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"0.11.2","version":"0.11.2"},"__v":0,"metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-15T18:05:08.294Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":1,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"We are living in a single page world\",\n  \"body\": \"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](https://angularjs.org/)\"\n}\n[/block]\n# Jinja2 extension for glim framework\nAfter 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\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pip install glim-extensions\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n# Configuration\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# app/config/<env>.py\\nconfig = {\\n    'extensions': {\\n       'view': {\\n           'package': 'app.views'\\n       },\\n    }\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\n\nThere 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;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# controllers.py\\nfrom glim_extensions.view import View\\nfrom glim import Controller\\nfrom app.models import Post\\nfrom glim_extensions.db import Orm\\n\\nclass PostController(Controller):\\n    def get(id):\\n        p = Orm.query(Post.post_id, Post.content).filter_by(post_id=id).first()\\n        if p:\\n            post = dict(zip(post.keys(), p))\\n            return View.render('post', post=post)\\n        else:\\n            return Response(status=404)\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nThe template file would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# views/post.html\\n<html>\\n    <body>\\n    Post id : {{ post.id }}\\n    Post content : {{ post.content }}\\n    </body>\\n</html>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Logic in templates\"\n}\n[/block]\n## Looping\n## If - Else Statements\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Including templates\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Extending templates\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sharing data between templates\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Configuring template path\"\n}\n[/block]","excerpt":"","slug":"views--templating","type":"basic","title":"Views & Templating"}

Views & Templating


[block:callout] { "type": "info", "title": "We are living in a single page world", "body": "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](https://angularjs.org/)" } [/block] # 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 [block:code] { "codes": [ { "code": "pip install glim-extensions", "language": "shell" } ] } [/block] # Configuration [block:code] { "codes": [ { "code": "# app/config/<env>.py\nconfig = {\n 'extensions': {\n 'view': {\n 'package': 'app.views'\n },\n }\n}", "language": "python" } ] } [/block] 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; [block:code] { "codes": [ { "code": "# controllers.py\nfrom glim_extensions.view import View\nfrom glim import Controller\nfrom app.models import Post\nfrom glim_extensions.db import Orm\n\nclass PostController(Controller):\n def get(id):\n p = Orm.query(Post.post_id, Post.content).filter_by(post_id=id).first()\n if p:\n post = dict(zip(post.keys(), p))\n return View.render('post', post=post)\n else:\n return Response(status=404)", "language": "python" } ] } [/block] The template file would be the following; [block:code] { "codes": [ { "code": "# views/post.html\n<html>\n <body>\n Post id : {{ post.id }}\n Post content : {{ post.content }}\n </body>\n</html>", "language": "html" } ] } [/block] [block:api-header] { "type": "basic", "title": "Logic in templates" } [/block] ## Looping ## If - Else Statements [block:api-header] { "type": "basic", "title": "Including templates" } [/block] [block:api-header] { "type": "basic", "title": "Extending templates" } [/block] [block:api-header] { "type": "basic", "title": "Sharing data between templates" } [/block] [block:api-header] { "type": "basic", "title": "Configuring template path" } [/block]