Python jinja2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python jinja2相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/html%e6%a8%a1%e6%9d%bf' title='html妯℃澘'>html妯℃澘   cell   line   port   搴忓垪   鐩稿   data   important   hat   

 

 

jinja2璇硶

鍩烘湰璇硶

 銆€銆€鍦╦inja2涓紝瀛樺湪涓夌璇硶锛?/p>

  1. 鎺у埗缁撴瀯 {% %}
  2. 鍙橀噺鍙栧€?{{ }}
  3. 娉ㄩ噴 {# #}

銆€銆€涓嬮潰鏄竴涓畝鍗曠殑jinja2渚嬪瓙

{# This is jinja code
    {% for file in filenames %}
    ...
    {% endfor %}
#}
鍙互鐪嬪埌锛宖or寰幆鐨勪娇鐢ㄦ柟寮忓拰Python姣旇緝绫讳技锛屼絾鏄病鏈変簡鍙ュ熬鐨勫啋鍙凤紝鍙﹀闇€瑕佷娇鐢╡ndfor鏈€涓虹粨灏撅紝鍏跺疄鍦╦inja2涓紝if涔熸槸涓€鏍风殑锛岀粨灏鹃渶瑕佷娇鐢╡ndif銆?/pre>

jinja2鍙橀噺

銆€銆€jinja2妯℃澘涓娇鐢?{{ }} 璇硶琛ㄧず涓€涓彉閲忥紝瀹冩槸涓€绉嶇壒娈婄殑鍗犱綅绗︺€傚綋鍒╃敤jinja2杩涜娓叉煋鐨勬椂鍊欙紝瀹冧細鎶婅繖浜涚壒娈婄殑鍗犱綅绗﹁繘琛屽~鍏?鏇挎崲锛宩inja2鏀寔python涓墍鏈夌殑Python鏁版嵁绫诲瀷姣斿鍒楄〃銆佸瓧娈点€佸璞$瓑銆?/p>

<p>this is a dicectory:{{ mydict[鈥?/span>key鈥?/span>] }} </p>
<p>this is a list:{{ mylist[3] }} </p>
<p>this is a object:{{ myobject.something() }} </p>

jinja2涓殑杩囨护鍣?/h2>

銆€銆€鍙橀噺鍙互閫氳繃鈥滆繃婊ゅ櫒鈥濊繘琛屼慨鏀癸紝杩囨护鍣ㄥ彲浠ョ悊瑙d负鏄痡inja2閲岄潰鐨勫唴缃嚱鏁板拰瀛楃涓插鐞嗗嚱鏁般€?/p>

銆€銆€甯哥敤鐨勮繃婊ゅ櫒鏈夛細

杩囨护鍣ㄥ悕绉?/td>     璇存槑    
safe  娓叉煋鏃跺€间笉杞箟
capitialize  鎶婂€肩殑棣栧瓧姣嶈浆鎹㈡垚澶у啓锛屽叾浠栧瓙姣嶈浆鎹负灏忓啓
 lower  鎶婂€艰浆鎹㈡垚灏忓啓褰㈠紡 
 upper  鎶婂€艰浆鎹㈡垚澶у啓褰㈠紡 
 title  鎶婂€间腑姣忎釜鍗曡瘝鐨勯瀛楁瘝閮借浆鎹㈡垚澶у啓
 trim  鎶婂€肩殑棣栧熬绌烘牸鍘绘帀
 striptags  娓叉煋涔嬪墠鎶婂€间腑鎵€鏈夌殑HTML鏍囩閮藉垹鎺?/td>
join   鎷兼帴澶氫釜鍊间负瀛楃涓?/td>
 replace  鏇挎崲瀛楃涓茬殑鍊?/td>
 round  榛樿瀵规暟瀛楄繘琛屽洓鑸嶄簲鍏ワ紝涔熷彲浠ョ敤鍙傛暟杩涜鎺у埗
int   鎶婂€艰浆鎹㈡垚鏁村瀷

 銆€銆€閭d箞濡備綍浣跨敤杩欎簺杩囨护鍣ㄥ憿锛?鍙渶瑕佸湪鍙橀噺鍚庨潰浣跨敤绠¢亾(|)鍒嗗壊锛屽涓繃婊ゅ櫒鍙互閾惧紡璋冪敤锛屽墠涓€涓繃婊ゅ櫒鐨勮緭鍑轰細浣滀负鍚庝竴涓繃婊ゅ櫒鐨勮緭鍏ャ€?/p>

{{ 鈥?/span>abc鈥?/span> | captialize  }}
# Abc
{{ 鈥?/span>abc鈥?/span> | upper  }}
# ABC
{{ 鈥?/span>hello world鈥?/span> | title }} # Hello World {{ "hello world" | replace(鈥?/span>world鈥?/span>,鈥?/span>daxin鈥?/span>) | upper }} # HELLO DAXIN {{ 18.18 | round | int }} # 18

jinja2鐨勬帶鍒剁粨鏋?/h2>

銆€銆€jinja2涓殑if璇彞绫讳技涓嶱ython鐨刬f璇彞锛屽畠涔熷叿鏈夊崟鍒嗘敮锛屽鍒嗘敮绛夊绉嶇粨鏋勶紝涓嶅悓鐨勬槸锛屾潯浠惰鍙ヤ笉闇€瑕佷娇鐢ㄥ啋鍙风粨灏撅紝鑰岀粨鏉熸帶鍒惰鍙ワ紝闇€瑕佷娇鐢╡ndif鍏抽敭瀛椼€?/p>

{% if daxin.safe %}
daxin is safe.
{% elif daxin.dead %}
daxin is dead
{% else %}
daxin is okay
{% endif %}

jinja2鐨刦or寰幆

銆€銆€jinja2涓殑for寰幆鐢ㄤ簬杩唬Python鐨勬暟鎹被鍨嬶紝鍖呮嫭鍒楄〃锛屽厓缁勫拰瀛楀吀銆傚湪jinja2涓笉瀛樺湪while寰幆銆?/p>

銆€銆€杩唬鍒楄〃

<ul>
{% for user in users %}
<li>{{ user.username|title }}</li>
{% endfor %}
</ul>

銆€銆€杩唬瀛楀吀

<dl>
{% for key, value in my_dict.iteritems() %}
<dt>{{ key }}</dt>
<dd>{{ value}}</dd>
{% endfor %}
</dl>

      褰撶劧涔熷彲浠ュ姞鍏lse璇彞锛屽湪寰幆姝g‘鎵ц瀹屾瘯鍚庯紝鎵ц

      鍦╢or寰幆涓紝jinja2杩樻彁渚涗簡涓€浜涚壒娈婄殑鍙橀噺锛岀敤浠ユ潵鑾峰彇褰撳墠鐨勯亶鍘嗙姸鎬侊細

鍙橀噺 鎻忚堪
loop.index 褰撳墠杩唬鐨勭储寮曪紙浠?寮€濮嬶級
loop.index0 褰撳墠杩唬鐨勭储寮曪紙浠?寮€濮嬶級
loop.first 鏄惁鏄涓€娆¤凯浠o紝杩斿洖bool
loop.last 鏄惁鏄渶鍚庝竴娆¤凯浠o紝杩斿洖bool
loop.length 搴忓垪涓殑椤圭洰鏁伴噺
loop.revindex 鍒板惊鐜粨鏉熺殑娆℃暟锛堜粠1寮€濮嬶級
loop.revindex0 鍒板惊鐜粨鏉熺殑娆℃暟(浠?寮€濮嬶級

jinja2鐨勫畯

銆€銆€瀹忕被浼间簬Python涓殑鍑芥暟锛屾垜浠湪瀹忎腑瀹氫箟琛屼负锛岃繕鍙互杩涜浼犻€掑弬鏁帮紝灏卞儚Python涓殑鍑芥暟涓€鏍蜂竴鏍峰効鐨勩€?/p>

銆€銆€鍦ㄥ畯涓畾涔変竴涓畯鐨勫叧閿瓧鏄痬acro锛屽悗闈㈣窡鍏?瀹忕殑鍚嶇О鍜屽弬鏁扮瓑

{% macro input(name,age=18) %}   # 鍙傛暟age鐨勯粯璁ゅ€间负18
 
 <input type=鈥?/span>text鈥?/span> name="{{ name }}" value="{{ age }}" >
 
{% endmacro %}
 

銆€銆€璋冪敤鏂规硶涔熷拰Python鐨勭被浼?/p>

<p>{{ input(鈥?/span>daxin鈥?/span>) }} </p>
<p>{{ input(鈥?/span>daxin鈥?/span>,age=20) }} </p>

jinja2鐨勭户鎵垮拰Super鍑芥暟

銆€銆€jinja2涓渶寮哄ぇ鐨勯儴鍒嗗氨鏄ā鏉跨户鎵裤€傛ā鏉跨户鎵垮厑璁告垜浠垱寤轰竴涓熀鏈?楠ㄦ灦)鏂囦欢锛屽叾浠栨枃浠朵粠璇ラ鏋舵枃浠剁户鎵匡紝鐒跺悗閽堝鑷繁闇€瑕佺殑鍦版柟杩涜淇敼銆?/p>

銆€銆€jinja2鐨勯鏋舵枃浠朵腑锛屽埄鐢╞lock鍏抽敭瀛楄〃绀哄叾鍖呮兜鐨勫唴瀹瑰彲浠ヨ繘琛屼慨鏀广€?/p>

銆€銆€浠ヤ笅闈㈢殑楠ㄦ灦鏂囦欢base.html涓轰緥锛?/p>

<!DOCTYPE html>
<html lang="en">
<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 %}
    <script>This is javascript code </script>
    {% endblock %}
</div>
</body>
</html>

銆€銆€杩欓噷瀹氫箟浜嗗洓澶?nbsp;block锛屽嵆锛歨ead锛宼itle锛宑ontent锛宖ooter銆傞偅鎬庝箞杩涜缁ф壙鍜屽彉閲忔浛鎹㈠憿锛熸敞鎰忕湅涓嬮潰鐨勬枃浠?/p>

{% extend "base.html" %}       # 缁ф壙base.html鏂囦欢
 
{% block title %} Dachenzi {% endblock %}   # 瀹氬埗title閮ㄥ垎鐨勫唴瀹?/span>
 
{% block head %}
    {{  super()  }}        # 鐢ㄤ簬鑾峰彇鍘熸湁鐨勪俊鎭?/span>
    <style type=鈥?/span>text/css鈥?/span>>
    .important { color: #FFFFFF }
    </style>
{% endblock %}   
 
# 鍏朵粬涓嶄慨鏀圭殑鍘熷皝涓嶅悓鐨勭户鎵?/span>

銆€銆€PS: super()鍑芥暟 琛ㄧず鑾峰彇block鍧椾腑瀹氫箟鐨勫師鏉ョ殑鍐呭銆?/p>

妯℃澘濉厖绀轰緥

涓€銆佸噯澶囨ā鏉?/strong>template.html:

<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
            <p style=鈥?/span>font-size:15px; font-family:Arial;鈥?/span>>{{ content }}</p>
            <table border="1" cellspacing="0" cellpadding="0">
            <tr>
                {% if array_table_head %}
                {% for var_i in array_table_head %}
                    <th style="font-size: 15px; padding: 3px;">{{var_i}}</th>
                {% endfor %}
                {% endif %}
            </tr>
            {% if dict_table_data %}
            {% for table_data in dict_table_data %}
            <tr>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Name }}</th>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Type }}</th>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Value }}</th>
            </tr>
            {% endfor %}
            {% endif %}
            </table>
    </body>
</html>

浜屻€佸姞杞芥ā鏉?/strong>

鏈変簡涓婅堪鐨刪tml妯℃澘锛屽悗鍙板埄鐢ㄥ涓嬩唬鐮佽鍏ャ€?/p>

import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader(鈥?/鈥?)
temp = env.get_template(鈥?/span>template.html鈥?/span>)

娉ㄦ剰涓€鐐? 鍏朵腑path闇€瑕佷负褰撳墠python鏂囦欢鎵€鍦ㄧ洰褰曠殑瀹屾暣璺緞锛実et_template鍐呴儴鐨勫弬鏁颁负html妯℃澘鐩稿浜庤python鏂囦欢鎵€鍦ㄧ洰褰曠殑璺緞(鐩稿璺緞)銆?/p>

涓夈€佹ā鎷熸暟鎹紝瀵规ā鏉胯繘琛孯ender

閫氳繃绗竴閮ㄥ垎鐨刪tml妯℃澘涓垜浠笉闅惧彂鐜拌妯℃澘涓€鍏遍渶瑕佷笁涓彉閲忥紝content銆?array_table_head 浠ュ強 dict_table_data銆傛墍浠ユ垜浠渶瑕佸湪鍚庡彴瀵硅繖涓変釜鍙橀噺杩涜妯℃嫙銆?/p>

1. 绫诲瀷鍒嗘瀽銆傞渶瑕佹敞鎰忕殑鏄紝鍙橀噺鐨勭被鍨嬩竴瀹氳鎶婃帶濂斤紝浠庢ā鏉跨殑瑙傚療鍙互鐪嬪嚭content鏄洿鎺ョ敤{{ }}鍖呰9鏉ュ紩鐢ㄧ殑锛屾墍浠ュ湪鍚庡彴搴旇鏄竴绉嶅彲浠ョ洿鎺ュ彇鍊肩殑绫诲瀷锛屼緥濡俿tr锛?int绛夈€傝€宎rray_table_head鏄€氳繃閬嶅巻鏉ュ紩鐢ㄧ殑锛岃鏄庢渶澶栧眰鍦ㄥ悗鍙版槸涓€涓狶ist鎴杢uple绛夊彲閬嶅巻瀵硅薄锛屽叾娆″湪鍐呭眰鏄洿鎺ュ彇鍊肩殑锛屾墍浠ュ悗鍙板簲璇ユ槸涓€涓畝鍗曠殑str鎴栬€卛nt鐨勫垪琛ㄣ€傚悓鐞嗭紝瀵逛簬dict_table_data, 鍒欐槸涓€涓瓧鍏稿瓧绗︿覆锛屾墍浠ヤ笁涓彉閲忕殑妯℃嫙搴旇濡備笅:

    render_dict = {}
    dict_table_data = [{鈥?/span>Name鈥?/span>: 鈥?/span>Basketball鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                       {鈥?/span>Name鈥?/span>: 鈥?/span>Football鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 4.5},
                       {鈥?/span>Name鈥?/span>: 鈥?/span>Pencil鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Learning鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                       {鈥?/span>Name鈥?/span>: 鈥?/span>Hat鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Wearing鈥?/span>, 鈥?/span>Value鈥?/span>: 2}]
    render_dict.update({鈥?/span>Content鈥?/span>: 鈥?/span>Hello reader, here is a table:鈥?/span>,
                        鈥?/span>array_table_head鈥?/span>: [鈥?/span>Name鈥?/span>, 鈥?/span>Type鈥?/span>, 鈥?/span>Value鈥?/span>],
                        鈥?/span>dict_table_data鈥?/span>: dict_table_data})

2. 妯℃澘娓叉煋

鏈€鍚庝竴姝ワ紝鍗抽€氳繃render鏂规硶灏嗗彉閲忔斁鍏ユā鏉夸腑锛岀劧鍚庣敓鎴愭柊鐨刪tml鍐欏叆鏂囦欢锛屾鏃讹紝妯℃澘璇█灏嗕細鍏ㄩ儴琚浆鍖栦负html銆?/p>

temp_out = temp.render(content=render_dict[鈥?/span>Content鈥?/span>], 
                       array_table_head=render_dict[鈥?/span>array_table_head鈥?/span>],
                       dict_table_data=render_dict[鈥?/span>dict_table_data鈥?/span>])
with open(os.path.join(鈥?/鈥? 鈥?/span>out.html鈥?/span>), 鈥?/span>w鈥?/span>, encoding=鈥?/span>utf-8鈥?/span>) as f:
    f.writelines(temp_out)
    f.close()
瀹屾暣瀹炰緥锛?/span>
import jinja2
import os
env = jinja2.Environment(loader=jinja2.FileSystemLoader(鈥?/span>./鈥?/span>))
temp = env.get_template(鈥?/span>template.html鈥?/span>)

render_dict = {}
dict_table_data = [{鈥?/span>Name鈥?/span>: 鈥?/span>Basketball鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Football鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 4.5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Pencil鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Learning鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Hat鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Wearing鈥?/span>, 鈥?/span>Value鈥?/span>: 2}]
render_dict.update({鈥?/span>Content鈥?/span>: 鈥?/span>Hello reader, here is a table:鈥?/span>,
                    鈥?/span>array_table_head鈥?/span>: [鈥?/span>Name鈥?/span>, 鈥?/span>Type鈥?/span>, 鈥?/span>Value鈥?/span>],
                    鈥?/span>dict_table_data鈥?/span>: dict_table_data})


temp_out = temp.render(content=render_dict[鈥?/span>Content鈥?/span>],
                       array_table_head=render_dict[鈥?/span>array_table_head鈥?/span>],
                       dict_table_data=render_dict[鈥?/span>dict_table_data鈥?/span>])
with open(os.path.join(鈥?/span>./鈥?/span>, 鈥?/span>out.html鈥?/span>), 鈥?/span>w鈥?/span>, encoding=鈥?/span>utf-8鈥?/span>) as f:
    f.writelines(temp_out)
    f.close()

鍙﹀涓€绉嶆柟寮忥細

from jinja2 import Template


TPL = 鈥樷€樷€?/span>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
            <p style=鈥榝ont-size:15px; font-family:Arial;鈥?gt;{{ content }}</p>
            <table border="1" cellspacing="0" cellpadding="0">
            <tr>
                {% if array_table_head %}
                {% for var_i in array_table_head %}
                    <th style="font-size: 15px; padding: 3px;">{{var_i}}</th>
                {% endfor %}
                {% endif %}
            </tr>
            {% if dict_table_data %}
            {% for table_data in dict_table_data %}
            <tr>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Name }}</th>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Type }}</th>
                <th style="font-size: 12px; padding: 3px;">{{ table_data.Value }}</th>
            </tr>
            {% endfor %}
            {% endif %}
            </table>
    </body>
</html>
鈥樷€樷€?/span>

render_dict = {}
dict_table_data = [{鈥?/span>Name鈥?/span>: 鈥?/span>Basketball鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Football鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Sports鈥?/span>, 鈥?/span>Value鈥?/span>: 4.5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Pencil鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Learning鈥?/span>, 鈥?/span>Value鈥?/span>: 5},
                   {鈥?/span>Name鈥?/span>: 鈥?/span>Hat鈥?/span>, 鈥?/span>Type鈥?/span>: 鈥?/span>Wearing鈥?/span>, 鈥?/span>Value鈥?/span>: 2}]
render_dict.update({鈥?/span>Content鈥?/span>: 鈥?/span>Hello reader, here is a table:鈥?/span>,
                    鈥?/span>array_table_head鈥?/span>: [鈥?/span>Name鈥?/span>, 鈥?/span>Type鈥?/span>, 鈥?/span>Value鈥?/span>],
                    鈥?/span>dict_table_data鈥?/span>: dict_table_data})


content = Template(TPL).render(render_dict)
with open(鈥?/span>out.html鈥?/span>, "w") as f:
        f.write(content)  # 鍐欏叆鏂囦欢


以上是关于Python jinja2的主要内容,如果未能解决你的问题,请参考以下文章

未找到 GAE Web 表单图像(使用 Python 和 jinja2)

12_关于flask中的宏

003:Python flask引入jinja2.exceptions.TemplateNotFound出错

Flask之模板之宏继承包含

从 jinja2 调用 python 函数

网站后端_Python+Flask.0014.FLASK模版相关之使用JinJa2模版渲染?