Template internal assignment-set

Sometimes you need to assign a value inside the template, this time you need to use setit:

grammar

{% set 变量名='值' %}

use

indexCreate a new myset.htmlfile in the folder , the code:

<!DOCTYPE html><html lang="en"><head>
    <meta charset='UTF-8'>
    <title>set赋值</title></head><body>
    {% set name='孟' %}    <p>{
   
   { name }}</p></body></html>

Then we app.pycreate a new function in:

@app.route('/myset/')def myset():
    return flask.render_template('index/myset.html')

Execute the file and visit the address, .../myset/you can see the <p>label content is printed out:

Assign values ​​in this way, then the variable in the entire file is visible. If we don’t want to pollute the global variable because of the assignment somewhere, we can also perform local assignment:

grammar

{% with 变量名='值' %}
...代码块...
{% endwith %}

or

{% with %}
    {% set 变量名='值' %}
    ...代码块...
{% endwith %}

If you exchange experience in software testing, interface testing, automated testing, and interviews. If you are interested, you can add software test communication: 1085991341, and there will be technical exchanges with colleagues.

use

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Title</title></head><body>
    {% set name='孟' %}    <p>{
   
   { name }}</p>        
    {% with class='Flask教程' %}        <p>{
   
   { class }}</p>
    {% endwith %}            <p>with 外面的{
   
   { class }}</p>    
    {% with %}
        {% set grade='初级' %}   
        <p>{
   
   { grade }}</p>
    {% endwith %}</body></html>

Save it to see the page: The

above content hopes to be helpful to you, friends who have been helped are welcome to like and comment.

Guess you like

Origin blog.csdn.net/Chaqian/article/details/106431478