flask 宏的定义和使用

一、关于Flask中的宏

类似于python中的函数,宏的作用就是在模板中重复利用代码,避免代码冗余。
Jinja2支持宏,还可以导入宏,需要在多处重复使用的模板代码片段可以写入单独的文件,再包含在所有模板中,以避免重复。

二、宏的定义和使用

1、不带参数宏的定义和使用

<!-- 定义, 相当于定义一个函数一样 -->
{
    
    % macro input() %}
    <input type="text" name='username' value=''>
{
    
    % endmacro %}

<!-- 使用, 相当于调用一个函数一样 -->
{
    
    {
    
     input() }}

2、带参数宏的定义和使用 (与函数是一样的,可以有必须参数和默认参数)

<!-- 定义 -->
{
    
    % macro input2(name, value='', type='text', size=30) %}
    <input type="{
    
    { type }}" name="{
    
    { name }}" value="{
    
    { value }}" size="{
    
    { size }}">
{
    
    % endmacro %}

<!-- 使用 -->
{
    
    {
    
     input2() }}   <!--name不指定, 则name="", 即也是空-->
<!-- 或者 -->
{
    
    {
    
     input2('username') }}
<!-- 或者 -->
{
    
    {
    
     input2('username', value='cheng', type='password', size=50) }}

三、导入宏和使用导入的宏 (import)

  • 在真实的开发中,会将一些常用的宏单独放在一个文件中,在需要使用的时候,再从这个文件中进行导入。
  • import语句的语法跟python的import类似,
    • 可以直接import … as …,
    • 也可以from … import …
    • 或者from … import … as …

1、import ‘宏文件的路径’ as xxx [with context]

  • with context 导入的时候,把当前模板文件中的所有上下文变量也传递到宏文件中,这样宏文件就可以使用这些变量了

2、from ‘宏文件的路径’ import 宏的名字 [as xxx]

3、如果想要在导入宏的时候,就把当前模板中一些上下文变量传给宏所在的模板,那么就应该在导入宏的时候使用with contex.

import 'macro.html' as func with context
form 'macor.html' import input with context

四、实例

1、宏定义

  • ./templates/marco.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>宏定义</title>
</head>
<body>
	<!--不带参数宏的定义-->
    {
    
    % macro input() %}
    <input type="text" name='username' value=''>
    {
    
    % endmacro %}
	
	<!--带参数宏的定义-->
    {
    
    % macro input2(name, value='', type='text', size=30) %}
        <input type="{
    
    { type }}" name="{
    
    { name }}" value="{
    
    { value }}" size="{
    
    { size }}">
    {
    
    % endmacro %}
</body>
</html>

2、导入宏 :

  • 为了方便区分,在两个文件用不同方法导入,可以合在一个文件里导入
  • /templates/demo_marco1.html
{
    
    % import 'marco.html' as macro with context %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>marco使用演示</title>
</head>
<body>
{
    
    {
    
     macro.input() }}
</body>
</html>
  • ./templates/demo_marco2.html
{
    
    % from 'marco.html'  import input2 with context %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>marco演示2</title>
</head>
<body>
<p>输入用户名:{
    
    {
    
     input2('username', value=username, type='text', size=20) }}</p>
<p>输入密码:{
    
    {
    
     input2('passwd', value='', type='password', size=10) }}</p>
<p>输入年龄:{
    
    {
    
     input2('age', value='18', type='text', size=5) }}</p>
</body>
</html>

3、使用宏:

from flask import Flask,render_template

app=Flask(__name__)

context={
    
    
    'username': '老萝卜',
    'age': 18
}

@app.route("/")
def demo_marco1():
    return render_template('demo_marco1.html', **context)

@app.route("/demo2")
def demo_marco2():
    return render_template('demo_marco2.html', **context)



if __name__=="__main__":
    app.run(debug=True)

4、效果

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/laoluobo76/article/details/109114925