{% block heading %}
{% block page %}{% endblock %} - Flask Super Example
{% endblock %}
This is part of my base template
{% block content %}{% endblock %}
This is part of my base template
* {% block trailer %}
* Watch! This will be added to my base and child templates using the super powerful super block!
*
* {% endblock %}
```
.center[t-layout-3.html (partial)]
]]
---
class: split-50 nopadding
.column_t2[.vmiddle[
# Example t-3
```html
{% macro nav_link(endpoint, name) %}
{{name}}
{% endmacro %}
```
.center[t-macro-3.html]
]]
.column_t1[.vmiddle[
```html
*{% from "t-macro-3.html" import nav_link with context %}
...
*{% block head %}
*
{% block title %}{% endblock %} - Flask Super Example
*{% endblock %}
...
* {{ nav_link('home', 'Home') }}
* {{ nav_link('about', 'About') }}
* {{ nav_link('contact', 'Contact Us') }}
-
...
```
.center[t-layout-3.html (partial)]
]]
---
class: column_t1 middle center
.figstyle1[

]
### output-t-3.html
---
class: split-30 nopadding
background-image: url(https://cloud.githubusercontent.com/assets/4231611/11557577/f6c9918e-99df-11e5-9c63-0bb70768fadf.jpg)
.column_t2.center[.vmiddle[
.fgtransparent[
#
]
]]
.column_t2[.vmiddle.nopadding[
.shadelight[.boxtitle1[
# Jinja + Flask
####
]]
]]
---
class: split-50 nopadding
.column_t2[.vmiddle[
```
from flask import Flask, render_template
import datetime
app = Flask(__name__)
#---------------------------------------------
*@app.template_filter()
*def datetimefilter(value, format='%Y/%m/%d %H:%M'):
return value.strftime(format)
*app.jinja_env.filters['datetimefilter'] = datetimefilter
#---------------------------------------------
*@app.route("/")
def template_test():
return render_template('f-template.html', my_string="Wheeeee!", my_list=[0,1,2,3,4,5], title="Index", current_time=datetime.datetime.now())
*@app.route("/home")
def home():
return render_template('f-template.html', my_string="Foo", my_list=[6,7,8,9,10,11], title="Home", current_time=datetime.datetime.now())
*@app.route("/about")
def about():
return render_template('f-template.html', my_string="Bar", my_list=[12,13,14,15,16,17], title="About", current_time=datetime.datetime.now())
*@app.route("/contact")
def contact():
return render_template('f-template.html', my_string="FooBar", my_list=[18,19,20,21,22,23], title="Contact Us", current_time=datetime.datetime.now())
#---------------------------------------------
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
```
]]
.column_t1[.vmiddle[
# Example #4
```html
{% macro nav_link(endpoint, name) %}
*{% if request.endpoint.endswith(endpoint) %}
{{name}}
*{% else %}
{{name}}
*{% endif %}
{% endmacro %}
```
.center[f-macro.html]
]]
---
class: split-50 nopadding
.column_t1[.vmiddle[
# Example #4
]]
.column_t2[.vmiddle[
```html
{% extends "f-layout.html" %}
*{% block title %}{{title}}{% endblock %}
*{% block head %} {{ super() }} {% endblock %}
*{% block page %}{{title}}{% endblock %}
*{% block heading %} {{ super() }} {% endblock %}
{% block content %}
This is the start of my child template
*
Current date/time: {{ current_time | datetimefilter }}
My string: {{my_string}}
Value from the list: {{my_list[3]}}
Loop through the list:
* {% for n in my_list %}
- {{n}}
* {% endfor %}
*
Same list with a filter: {{ my_list|join(', ') }}
This is the end of my child template
* {% block trailer %} {{super()}} {% endblock %}
{% endblock %}
```
.center[f-template.html]
]]
---
class: column_t1 middle center
.figstyle1[

]
### http://localhost:5000
---
class: column_t1 middle center
.figstyle1[

]
### http://localhost:5000/about
---
# References
1. [Welcome to Jinja2 — Jinja2 Documentation](http://jinja.pocoo.org/docs/dev/)
1. [Introduction](http://jinja.pocoo.org/docs/dev/intro/)
1. [Tips and Tricks](http://jinja.pocoo.org/docs/dev/tricks/)
1. [Template Designer](http://jinja.pocoo.org/docs/dev/templates/)
1. [Primer on Jinja Templating - Real Python](https://realpython.com/blog/python/primer-on-jinja-templating/)
# Other Readings
1. [Jinja2 Example | Python Adventures](https://pythonadventures.wordpress.com/2014/02/25/jinja2-example-for-generating-a-local-file-using-a-template/)
1. [Jinja2 Examples](https://gist.github.com/wrunk/1317933)
1. [Quickstart Guide to Using Jinja2](http://kagerato.net/articles/software/libraries/jinja-quickstart.html)
---
class: split-30 nopadding
background-image: url(https://cloud.githubusercontent.com/assets/4231611/11557577/f6c9918e-99df-11e5-9c63-0bb70768fadf.jpg)
.column_t2.center[.vmiddle[
.fgtransparent[
#
]
]]
.column_t2[.vmiddle.nopadding[
.shadelight[.boxtitle1[
# END
### [Eueung Mulyana](https://github.com/eueung)
### http://eueung.github.io/python/jinja2
#### Python CodeLabs | [Attribution-ShareAlike CC BY-SA](https://creativecommons.org/licenses/by-sa/4.0/)
####
]]
]]