• python web模板 jinja2


    模板继承

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html lang="en">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        {% block head %}
        <link rel="stylesheet" href="style.css" />
        <title>{% block title %}{% endblock %} - My Webpage</title>
        {% endblock %}
    </head>
    <body>
        <div id="content">{% block content %}{% endblock %}</div>
        <div id="footer">
            {% block footer %}
            &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
            {% endblock %}
        </div>
    </body>
    </html>

    子模板

        {% extends "base.html" %}
        {% block title %}Index{% endblock %}
        {% block head %}
            {{ super() }}
            <style type="text/css">
                .important { color: #336699; }
            </style>
        {% endblock %}
        {% block content %}
            <h1>Index</h1>
            <p class="important">
              Welcome on my awesome homepage.
            </p>
        {% endblock %}

    Delimiters(分隔符)

    {% ... %} 语句(Statements)
    {{ ... }} 打印模板输出的表达式(Expressions)
    {# ... #} 注释
    # ... ## 行语句(Line Statements)

    Variables(变量)

    {{ mydict['key'] }}
    {{ mylist[3] }}
    {{ mylist[myintvar] }}
    {{ myobj.somemethod() }}
    {{ foo.bar }}
    {{ foo['bar'] }}

    for循环

        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
            {{loop.last}}
        {% endfor %}
        loop.index        当前循环迭代的次数(从 1 开始)
        loop.index0        当前循环迭代的次数(从 0 开始)
        loop.revindex    到循环结束需要迭代的次数(从 1 开始)
        loop.revindex0    到循环结束需要迭代的次数(从 0 开始)
        loop.first        如果是第一次迭代,为 True 。
        loop.last        如果是最后一次迭代,为 True 。
        loop.length        序列中的项目数。
        loop.cycle        在一串序列间期取值的辅助函数。见下面的解释。

    if条件语句

    1     {% extends layout_template if layout_template is defined else 'master.html' %}
    2     
    3     {% if kenny.sick %}
    4         Kenny is sick.
    5     {% elif kenny.dead %}
    6         You killed Kenny!  You bastard!!!
    7     {% else %}
    8         Kenny looks okay --- so far
    9     {% endif %}

    包含

    {% include 'header.html' %}
    {% include "sidebar.html" ignore missing %} [(Jinja 2.2) 如果模板不存在,Jinja 会忽略这条语句]
    {% include ['special_sidebar.html', 'sidebar.html'] ignore missing %}

    Filter(过滤器)

        {{items|length}}         返回长度
        {{ items|join(', ') }}    过滤器用来修改变量,使用一个竖线和变量相隔
        {{items|safe}}             渲染时不转义
        {{items|capitalize}}     首字母大写
        {{items|lower}}     小写
        {{items|upper}}     大写
        {{items|title}}     每个单词的首字母都转换成大写
        {{items|trim}}         去掉首尾空格
        {{items|striptags}} 去掉值里的HTML标签
        default 设置一个默认值,如果变量未定义,就用这个默认值替换。类似这样:
        {{ my_variable|default('my_variable is not defined') }}
  • 相关阅读:
    java ssh免密登录
    [8.0][MGR][bug]多主模式,外键冲突错误
    内核月报bookmark
    netcat 传输T级别大文件
    innodb部分内部操作
    qps.sh
    ABAP-ALV判断骚操作
    HCM基本知识
    SAP-VOFM的使用
    ABAP-处理去掉特殊字符
  • 原文地址:https://www.cnblogs.com/boye169/p/14993073.html
Copyright © 2020-2023  润新知