宏的定义与使用
说明:宏和python中的函数类似,可以传递参数。但是不能有返回值,可以把经常用到的代码片段放在宏中,然后把一些不固定的值抽出来作为一个变量。
宏的定义:
<!--定义宏--->
<% macro input(name,type='text',value=' ')%>
<input type="{
{type}}"name="{
{name}}"value="{
{value}}">
<% endmacro %>
宏的调用:
<!--调用宏--->
{
{input('username') }}
{
{input('password',type='password')}}
示列:
1.创建一个index-1。html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!---定义宏--->
{% macro input(name,type='text',value='',size=20)-%}
<input name="{
{name}}" type="{
{type}}" value="{
{value}}" size="{
{size}}" maxlength="8">
{%- endmacro %}
<div>
<!---调用宏--->
<p>用户名:{
{input(name='username',type='text',size=25)}}</p>
<p>密码:{
{input(name='password',type='password',value='请输入密码',size=20)}}</p>
<p>登录:{
{input(name='denglu',type='button',value='登录',size=20)}}</p>
</div>
</body>
</html>
2。创建一个xxx.py文件
from flask import Flask,render_template
import random
app=Flask(__name__)
@app.route('/')
def index():
return render_template('index-1.html')
if __name__ == '__main__':
app.run(debug=Tr