# 25 宏的概念和基本使用
模块中的宏跟python中的函数类似,可以传递参数,但是不能有返回值,可以将一些经常用到的代码片段放到宏中,然后把一些不固定的值抽取出来当成一个变量。
```text
{% marco 函数%}
函数
{% endmarco %}
```
```text
app.py
from flask import Flask, render_template
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
@app.route('/')
def hello_world():
return render_template("index/index.html",username="angle")
if __name__ == '__main__':
app.run(debug=True)
```
```text
index.html
{% import "macros/macros.html" as macros %}
宏登录
用户名: | {{ macros.input("username") }} |
密码: | {{ macros.input(name="password",type="password") }} |
{{ macros.input(value="提交",type="submit") }} |
```
```text
macors.html
{% macro input(name="",value="",type="text") %}
{% endmacro %}
```
![](https://box.kancloud.cn/d9341a569825f77a2cc2a98fd98343c0_444x359.png)