python background writer from zero items (c)

Flask last issue we wrote a little ditty, but that some too shabby, we make this page dynamic!

A, Jinja2 template engine to know (too lazy to look you can not see)

template

In the previous example, the primary effect is to function in response to the view generation request, which is the most simple request. In fact, the view function has two effects: the business logic processing and returns a response content. In large applications, business logic and presentation content together, it will increase the complexity and maintenance costs of the code. This learned template section, i.e., its role is to assume the role of another view of a function that returns a response content.

  • It is actually a response template file contains text which is represented by a placeholder (variable) dynamic part that tells the template specific values ​​obtained from the data engine needs to be used in

  • Substitution variables with real values, and then returns the resulting string, a process called "rendering"

  • Flask is Jinja2 use this template template engine to render
    the benefits of using templates:

  • The view function is only responsible for processing service logic and data (business logic terms)

  • And then take the template data on display function result view (view showing aspects)

  • Clear code structure, low coupling

Jinja2

Two concepts:

  • Jinja2: Python is a widely used template engine, is implemented by Python template language, his design ideas from Django template engine, and expanded its syntax and a powerful set of features that are built-in templates Flask language.
  • Template language: It is one kind of specific location is designed to automatically generate simple text document format in the template language, usually some variables passed to the template, replacing the pre-defined template placeholder variable name.

Render the template function

  • Flask render_template function package provides the template engine
  • The first parameter is the file name render_template function template, the latter parameters are key-value pairs, represent the true value of the variable in the corresponding template.

Second, violence Jinja2 learning template (Disclaimer: jinja2 is used in the inside of HTML)

Note

{# 我是注释 #}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{# 我是注释 #}
</body>
</html>

Variables block (incoming from the python, may be displayed on the page)

{{ name }}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{{ name }}
</body>
</html>

Control block (Analyzing and loops, small BB, look on the line)

{% if user %}
    {{ user }}
{% else %}
    hello!
{% for index in indexs %}
    {{ index }} 
{% endfor %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% if user %}
    {{ user }}
{% else %}
    hello!

{% for index in indexs %}
    {{ index }} 
{% endfor %}

</body>
</html>

filter

Variable can be modified by a "filter", a filter can be understood as jinja2 inside the built-in functions and string handling functions.

{{ 'abc' | captialize  }}
# Abc
 
{{ 'abc' | upper  }}
# ABC
 
{{ 'hello world' | title  }}
# Hello World


# 过滤器可以链式使用 
{{ "hello world" | replace('world','daxin') | upper }}
# HELLO DAXIN
 
{{ 18.18 | round | int }}
# 18

I will filter a little summary

first name Features
safe Render time when not escape
capitialize The first letter of the conversion value to uppercase, lowercase other picture
lower The conversion value to lowercase
upper The value is converted to uppercase
title The value of the first letter of each word are converted to uppercase
trim The value of the trailing spaces removed
striptags Before rendering the value of all the HTML tags are deleted
join Stitching a plurality of strings is
replace Alternatively value of the string
round The default rounding of numbers, parameters may be controlled by
int Converting to integer values

Show

from flask import Flask,render_template

app = Flask(__name__)

@app.route("/")
def index():
    names=["李雷","思远","彦林","辉辉","天骄"]
    strs="abcde"
    #第一个names是传入html用于接受的名字,第二个是python里变量的名字
    return render_template("index.html",names=names,strs=strs)
if __name__ == '__main__':
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% for name in names %}
        {% if name=="天骄" %}
            My love is {{ name }}!<br>

        {% else %}

            My friend is {{ name }}! <br>
        {% endif %}
    {% endfor %}

    {{ strs }}的大写是{{ strs | upper  }}

</body>
</html>

operation result:

Here Insert Picture Description

Today Jinja2 simply talk about grammar, I think it should be pretty easy to understand, tomorrow and even more behind the content!

Published 13 original articles · won praise 4 · Views 352

Guess you like

Origin blog.csdn.net/qq_43769745/article/details/104806685