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>
- 鎺у埗缁撴瀯 {% %}
- 鍙橀噺鍙栧€?{{ }}
- 娉ㄩ噴 {# #}
銆€銆€涓嬮潰鏄竴涓畝鍗曠殑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)