zoukankan      html  css  js  c++  java
  • 如何继承jinja模板

    1. be clear to what you are expecting : 我想知道如何在jinja2中实现模板继承

    首先要了解,block的概念,官网给了很清晰的解释:

    All the block tag does is tell the template engine that a child template may override those placeholders in the template.

    2.父模板中的代码:

    {%block title%}<p> this will appear if the lunchbox.html does nothing</p> {%endblock%}
    {%block description%} <p> this will appear if the lunchbox.html does nothing</p> {%endblock%}

    子模板中的代码:
    {% extends "/Users/WildChaser/EurekaMatata/Lunchbox/templates/homepage.html" %}
    {%block title%}
    {{title}}
    {%endblock%}

    {%block description%}
    {{description}}
    {%endblock%}

    3.注意点:子模板对父模板的block不过是起到一个override的作用。

    4.父模板是个request,子模板其实是response的内容,那么web服务器的可视函数要怎么写呢?

    jinja2的生命周期:先创建加载器,再创建envrionment对象,最后创建template对象。

    @app.route('/test',methods=['POST'])
    def helloworld():
    templateLoader = jinja2.FileSystemLoader(searchpath="/")
    templateEnv = jinja2.Environment(loader=templateLoader)

    TEMPLATE_FILE = "/Users/WildChaser/EurekaMatata/Lunchbox/templates/lunchbox2.html"
    template = templateEnv.get_template(TEMPLATE_FILE)

    dbc = Dbc.Dbc()
    result = dbc.selectAllMeal()
    dbc.closeDB()

    jfile = json.dumps(result)

    templateVars = {"title": jfile,
    "description": "A simple inquiry of function.",
    }

    outputText = template.render(templateVars)
    return outputText




  • 相关阅读:
    Ubuntu下虚拟机卡顿
    C函数重载
    ACPI电源状态定义
    /dev/random生成随机数慢的问题
    GCC强制静态库链接未使用的函数、变量
    cmake交叉编译问题
    cmake语法
    CentOS安装OpenOCD
    LOJ3119. 「CTS2019 | CTSC2019」随机立方体 二项式反演
    BZOJ4710 [Jsoi2011]分特产 容斥
  • 原文地址:https://www.cnblogs.com/wildchaser/p/5931268.html
Copyright © 2011-2022 走看看