Difference between revisions of "Jinja Template Stuff"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (→Zip) |
PeterHarding (talk | contribs) |
||
Line 1: | Line 1: | ||
=References= | |||
* http://jinja.pocoo.org/docs/switching/ | |||
* http://stackoverflow.com/questions/12485694/python-flask-and-jinja-templates-how-to-iterate-over-a-dictionary-created-se | * http://stackoverflow.com/questions/12485694/python-flask-and-jinja-templates-how-to-iterate-over-a-dictionary-created-se |
Revision as of 18:03, 7 July 2014
References
- http://stackoverflow.com/questions/12485694/python-flask-and-jinja-templates-how-to-iterate-over-a-dictionary-created-se
- http://stackoverflow.com/questions/16947276/flask-sqlalchemy-iterate-column-values-on-a-single-row
- http://www.realpython.com/blog/python/primer-on-jinja-templating/
Examples
{% for link in links %} <li><a href="{{ link }}">{{ link }}</a></li> {% endfor %}
Number of links is: {{ links|length }}
{% set myvar = "David" %} My variables: {{ myvar }} {% for link in links %} {{ myvar }} {% set myvar = 'Changed' %} {% endfor %} {{ myvar }}
<img src="Template:Url for('static', filename = 'css/images/icons/resultset previous.png')" width="16" height="16" alt="previous" title="Previous" border="0">
def tech_pro(name='Tech.Pro'): return render_template('message.html', **locals())
def tech_pro(name='Tech.Pro'): links = get_links() return render_template('message.html', **locals()) def get_links(): links = ['http://www.tech.pro/article1', 'http://www.tech.pro/article2', 'http://www.tech.pro/article3'] return links
<p> <h3>My Articles:</h3> <ul> {% for link in links %} <li><a href="{{ link }}">{{ link }}</a></li> {% endfor %} </ul> </p>
A More Complete Example
import os from flask import Flask from jinja2 import Environment, FileSystemLoader from random import randint # Start the Flask application app = Flask(__name__) # Define the template directory tpldir = os.path.dirname(os.path.abspath(__file__))+'/templates/' # Setup the template enviroment env = Environment(loader=FileSystemLoader(tpldir), trim_blocks=True) # Define a route for the webserver @app.route('/') def index(): # define a random skill level skill_level = randint(0,100) # generate template and assign variables output = env.get_template('example.html').render( skill_level=skill_level ) # return the output return output # Listen to port 80 if __name__ == '__main__': app.run( host="0.0.0.0", port=int("80") )
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title></title> <meta name="description" content="Using `if` statements in templates [Python, Flask and Jinja2]"> <meta name="author" content="Richard Ruiter"> </head> <body> <h1>Your skill level is:</h1> <p> {% if skill_level >= 0 and skill_level < 25 %} Beginner {% elif skill_level >= 25 and skill_level < 50 %} Intermediate {% elif skill_level >= 50 and skill_level < 75 %} Advanced {% else %} Expert {% endif %} </p> </body> </html>
Zip
you need zip() but it isn't defined in jinja2 templates.
one solution is zipping it before render_template function is called, like:
view function:
return render_template('form_result.html',type=type,reqIDs_msgs_rcs=zip(IDs,msgs,rcs))
template:
{% for reqID,msg,rc in reqIDs_msgs_rcs %} <h1>ID - {{ID}}</h1> {% if rc %} <h1>Status - {{msg}}!</h1> {% else %} <h1> Failed </h1> {% endif %} {% endfor %}
also, you can add zip to jinja2 template global, using Flask.add_template_x functions(or Flask.template_x decorators)
@app.template_global(name='zip') def _zip(*args, **kwargs): #to not overwrite builtin zip in globals return __builtins__.zip(*args, **kwargs)