Jinja2
Imagine a scenario where for a certain website, if you recharge your Vip, you can only see the hidden content now. What should you do?
This is where jinja2 comes in.
Write a code that renders a web page based on the name, then pass the name and vip variables into the HTML template.
Note that HTML pages using jinja2 cannot be opened directly in a browser, you must start Flask and use the route to return to the page.
from flask import Flask, render_template app = Flask(__name__) @('/space/<name>') def space(name): vip = True return render_template("", name=name, vip=vip) if __name__ == '__main__': (debug=True)
When the html template, which receives the flask passed thename、vip
parameter, how is it used in html?
- utilization
{{ }}
Load a variable, such as in the text using{{ name }}
Load in htmlname
variant - utilization
{% %}
Load a statement for an if statement formatted as in the following code8-11
line, it is necessary to take{% if xxx %}
in order to{% endif %}
Ending.
<!doctype html> <html lang="en"> <head> <title>Hello from Flask</title> </head> <body> <h1>Hello {{ name }}!</h1> <!-- jinja2(used form a nominal expression)ifstatement --> {% if vip %} <h3>yell (to urge on an animal),You triggered it.,Hide the content!!</h3> {% endif %} </body> </html>
Jinja2 statement extensions
Let's start with the final conclusion: jinja2 uses {{ }} for loading variables and {{% %}} for loading statements, and inside the parentheses it's all just written in the Python style!
In html, we load variables, and possibly a list, an object, a dictionary, and so on.
We loaded it as shown below:
# python a = [1,2,3] b = Cat(name="Tom") c = {"name":"Jackson"} # html <h1>Hello {{ a[0] }}!</h1> <h1>Hello {{ }}!</h1> <h1>Hello {{ c['name '] }}!</h1>
# Output results
Hello 1!
Hello Tom!
Hello Jackson!
In html, not only if statement should be used, there is a more important looping statement. jinja2's looping statement is shown below:
{% for item in navigation %} <li><a href="{{ }}" rel="external nofollow" >{{ }}</a></li> {% endfor %}
The code above generates thelen(navigation )
Each li points to theThe link.
It's all seen here, kudos White!
Jinja2 template inheritance
Jinja2's template inheritance actually has some similarities to object inheritance. For example, it can reduce a lot of redundant code. Next, I will show a small example of Jinja2 template inheritance:
We create a, write the following code:
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="" rel="external nofollow" /> <title>{% block title %}{% endblock %}</title> {% block head %}{% endblock %} </head> <body> <div >{% block body %}{% endblock %}</div> </body> </html>
In the above code, the{% block xxx %} {% endblock %}
Indicates the opening of a card slot to facilitate the filling of subsequent pages.
Next, write an inheritance page in the following manner:
{% extends "" %} {% block title %}home page (of a website){% endblock %} {% block body %} <h1>这里是home page (of a website)</h1> <p class="detail"> home page (of a website)的内容 </p> {% endblock %}
{% extends “” %}
Indicates an inheritance template
{% block title %}Home {% endblock %}
Indicates the use of the TITLE card slot.
The first page shows the contents of the slot, and the body slot is the same.
To this article on Flask in-depth understanding of the use of Jinja2 engine is introduced to this article, more related to Flask Jinja2 content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!