cs coe 1520
play

CS/COE 1520 pitt.edu/~ach54/cs1520 Using Templates to Generate - PowerPoint PPT Presentation

CS/COE 1520 pitt.edu/~ach54/cs1520 Using Templates to Generate Views in Flask Jinja2 templating language Jinja templates are simply text files Include Jinja tags that will be expanded when the template is rendered Here, we'll


  1. CS/COE 1520 pitt.edu/~ach54/cs1520 Using Templates to Generate Views in Flask

  2. Jinja2 templating language ● Jinja templates are simply text files ○ Include Jinja tags that will be expanded when the template is rendered ■ Here, we'll focus on using Jinja to produce HTML, however, it can be used to generate any kind of text document ● Flask configures the Jinja template engine for use within web apps automatically 2

  3. Basic Jinja tags ● {{ … }} ○ Expression tag, contents are evaluated and place in the text ● {% … %} ○ Statement tag, used to define Jinja constructs and issue flow control statements ● {# … #} ○ Comment 3

  4. Simple Jinja tag example <!doctype html> <html> <head> <title>Hello from Flask</title> </head> <body> {% if name %} <h1>Hello {{ name }}!</h1> {% else %} <h1>Hello, World!</h1> {% endif %} </body> </html> 4

  5. Using Jinja templates within Flask ● The render_template( template_name , arguments … ) function is used to grab a template file, and pass data to be used in generating a view of the page (e.g., name ) ○ By convention , Flask will look for templates in a directory simply called "templates" ○ In addition to arguments , the session and request Flask vars can be referenced in templates ■ And, additionally, others we have yet to discuss 5

  6. Control structures ● Operate similarly to their Python variants ○ {% if cond %} {% elif cond %} {% else %} {% endif %} ■ Only render a part of the template if some condition is met ● E.g., display logout link if a user is logged in ○ {% for i in seq %} {% endfor %} ■ Render some part of the template multiple times for each item in a given sequence ● E.g., create a div for each of a user's blog posts 6

  7. Template Inheritance ● Define blocks of a template that can be overridden in subtemplates ○ {% block name %} ○ {% endblock %} ● Establish inheritance through the {% extends base %} tag 7

  8. Message flashing ● In our examples, it would be handy to the let the use know they've been successfully logged in ○ Or give notice that there was an error in their attempt… ● Message flashing is built to handle this type of feedback! ● flash( msg ) adds msg to session["_flashes"] ○ Can be retrieved with get_flashed_messages() ■ Accessible within templates! 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend