1、include语句

最主要的作用是复用代码,可以将一个模板引入到另一个模板中,也就是完成”将一个模板中的代码复制到另一个模板中”的功能
小例子,现在有html1.html以及html2.html两个模板文件需要共用一部分的共用代码
代码部分

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. app.config["TEMPLATES_AUTO_RELOAD"] = True
  4. @app.route("/")
  5. def home():
  6. return "首页"
  7. @app.route("/html1/")
  8. def html1():
  9. return render_template("html1.html")
  10. @app.route("/html2/")
  11. def html2():
  12. return render_template("html2.html")
  13. if __name__ == "__main__":
  14. app.run(debug=True)

模板部分:在templates文件夹中创建commit文件夹用来存放共有代码部分footer.html以及header.html文件
footer.html

  1. <footer>这是网页底部</footer>

header.html

  1. <ul>
  2. <li>新闻</li>
  3. <li>电视</li>
  4. <li>综艺</li>
  5. </ul>

在新的模板中引入这些公有的模板
html1.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>html1</title>
  6. </head>
  7. <body>
  8. {% include 'commit/header.html' %}
  9. <h1>昨日青空</h1>
  10. {% include 'commit/footer.html' %}
  11. </body>
  12. </html>

html2.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>html2</title>
  6. </head>
  7. <body>
  8. {% include 'commit/header.html' %}
  9. <h1>流浪地球</h1>
  10. {% include 'commit/footer.html' %}
  11. </body>
  12. </html>

网页显示:
image.pngimage.png

2、set语句以及with语句

  • set语句相当于在当前模板中定义了一个全局变量,(但在py文件中有同名的变量时,优先使用set定义的变量)
  • with语句相当于在当前模板中定义了一个局部变量,作用范围从{% with %}开始到{% endwith %}结束
  • 变量的种类可以是数字、字符、字典、列表等等

代码部分:

  1. from flask import Flask, render_template
  2. app = Flask(__name__)
  3. app.config["TEMPLATES_AUTO_RELOAD"] = True
  4. dic = {
  5. "name": "python",
  6. "books": {
  7. "book1": "python",
  8. "book2": "java",
  9. "book3": "php"
  10. }
  11. }
  12. @app.route("/")
  13. def home():
  14. return "首页"
  15. @app.route("/set/")
  16. def set_html():
  17. return render_template("set.html", **dic)
  18. if __name__ == "__main__":
  19. app.run(debug=True)

模板部分:(有部分可以定义宏,但是我懒哈哈)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>set</title>
  6. </head>
  7. <body>
  8. <!--set相当于定义一个全局变量-->
  9. {% set name="这是set定义的变量" %}
  10. <p>{{ name }}</p>
  11. <hr>
  12. <!--with相当于定义一个局部变量,不要忘了endwith-->
  13. {% with books={"book1": "红楼梦", "book2": "三国演义", "book3": "西游记" }%}
  14. {% for book in books.values() %}
  15. <p>{{ book }}</p>
  16. {% endfor %}
  17. {% endwith %}
  18. <hr>
  19. <!--with定义的变量在这里失效了-->
  20. <!--此时调用的是py文件中传入的参数-->
  21. {% for book in books.values() %}
  22. <p>{{ book }}</p>
  23. {% endfor %}
  24. <hr>
  25. <!--变量的赋值可以是从py文件中导入-->
  26. {% with books=books%}
  27. {% for book in books.values() %}
  28. <p>{{ book }}</p>
  29. {% endfor %}
  30. {% endwith %}
  31. </body>
  32. </html>

页面显示:
image.png
总结三种定义方式:

  1. <!--第一种-->
  2. {% set xxx=xxx %}
  3. <!--第二种-->
  4. {% with %}
  5. {% set xxx=xxx %}
  6. xxxx
  7. {% endwith %}
  8. <!--第三种-->
  9. {% with xxx=xxx %}
  10. xxxx
  11. {% endwith %}