tags:
categories:
模板是呈现给用户的界面
在MVT中充当T的角色,实现了VT的解耦,开发中VT有这N:M的关系,一个V可以调用任意T,一个T可以被任意V调用
模板处理分为两个过程
1.加载
2.渲染
模板代码包含两个部分
1.静态HTML
2.动态插入的代码段
Flask中使用Jinja2模板引擎
Jinja2由Flask作者开发一个现代化设计和友好的Python模板语言,模仿Django的模板引擎
优点
速度快,被广泛使用
HTML设计和后端Python分离
减少Python复杂度
非常灵活,快速和安全
提供了控制,继承等高级功能
模板语法主要分为两种:变量和标签
<html lang="en">
<head>
<meta charset="UTF-8">
<title>StudentsListtitle>
head>
<body>
<ul>
{% for student in student_list %}
<li> {
{ student }} li>
{% endfor %}
{% for student in student_list %}
{% if loop.first %}
<li style="color: red"> {
{ loop.index }}:{
{ loop.index0 }}:{
{ student }} li>
{% elif loop.last %}
<li style="color: green"> {
{ loop.index }}:{
{ loop.index0 }}:{
{ student }} li>
{% else %}
<li style="color: grey"> {
{ loop.index }}:{
{ loop.index0 }}:{
{ student }} li>
{% endif %}
{% endfor %}
ul>
<hr>
{% if a == 5 %}
这是A
{% endif %}
body>
html>
@blue.route('/students/')
def students():
student_list = [i for i in range(10)]
return render_template('Students.html', student_list=student_list, a=5, b=5)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{
{ title }}title>
{% block ext_css %}
{% endblock %}
head>
<body>
<div>
{% block header %}
{% endblock %}
{% block content %}
{% endblock %}
{% block footer %}
{% endblock %}
div>
{% block ext_js %}
{% endblock %}
body>
html>
{% extends 'user/base_user.html' %}
{% block header %}
{% include 'banner.html' %}
{% endblock %}
{% block content %}
<h2>这是用户注册的内容h2>
{% macro hello(name) %}
<h2>这是一个hello函数h2>
{% endmacro %}
{
{ hello() }}
{
{ hello() }}
{
{ hello() }}
{% from 'html_func.html' import hehe, goods %}
{
{ hehe('小李') }}
{
{ goods('水杯') }}
{% endblock %}
{
% extends 'user/user_register.html' %}
<!--user/user_register2.html页面-->
{
% block content %}
<!--不覆盖前一个页面的内容-->
{
{
super() }}
<h2>二次填坑</h2>
{
% endblock %}
<div>
<h1>大字轮播h1>
div>
{% macro hehe(name) %}
<h1>哈哈: {
{ name }}h1>
{% endmacro %}
{% macro goods(name) %}
<h1>这有一个商品: {
{ name }}h1>
{% endmacro %}
@blue.route('/user_register')
def user_register():
return render_template('user/user_register.html', title='用户注册')
@blue.route('/user_register2')
def user_register2():
return render_template('user/user_register2.html', title='用户注册2')
{% extends 'bootstrap/base.html' %}
{% block navbar %}
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="#">标题a>
div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">首 页 <span class="sr-only">(current)span>a>li>
<li><a href="#">电影a>li>
<li><a href="#">电影院a>li>
ul>
<form class="navbar-form navbar-right">
<div class="form-group">
<input type="text" class="form-control" placeholder="用户名">
div>
<div class="form-group">
<input type="text" class="form-control" placeholder="密码">
div>
<button type="submit" class="btn btn-default">登录button>
form>
div>
div>
nav>
{% endblock %}
{% block content %}
{% block header %}
{% endblock %}
{% block container %}
{% endblock %}
{% block footer %}
{% endblock %}
{% endblock %}
{% extends 'base.html' %}
{% block header %}
<div class="container">
<div class="jumbotron">
<h1>Hello, world!h1>
<p>This is a simple hero unit, a simple jumbotron-style component for calling extra attention to featured content or information.p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn morea>p>
div>
div>
{% endblock %}
@blue.route('/index')
def index1():
return render_template('index.html')