Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
產生 HTML
|
699
jinja2.Environment 類別
當你使用
jinja2
,永遠都會涉及一個
Environment
實體,在少數情況中,你
可以讓它預設為一個通用的「共用環境(shared environment)」,但並不建
議那樣做。只有在非常進階的用法中,你會從不同的來源取得範本(或使
用不同的範本語言語法)時,你才會定義多個環境,通常,你會實體化單
一個
Environment
實體
env
,適用於你需要處理的所有範本。
你能在建置的時候以許多方式自訂
env
,方法是將具名引數傳入它的建構
器(包括更動範本語言語法的關鍵面向,例如哪些分隔符號起始和結束區
塊、變數、註解等等),但在真實世界的用例中,你幾乎永遠都會傳入的
一個具名引數是
loader=...
一個環境的
loader
指出從何處載入範本,在被請求的時候:通常是檔案系
統中的某個目錄,又或者是某個資料庫(對於後者,你必須編寫
jinja2.
Loader
的一個自訂的子類別),但還有其他的可能性。你需要一個載入
器(loader)來讓範本享有
jinja2
的某些強大的功能,例如
範本繼承
template inheritance
,本書中並沒有涵蓋)。
實體化的時候,你可用自訂的過濾器、測試、擴充元件等等的來裝備
env
那些也可以在之後新增,而我們不會在本書中進一步涵蓋它們。
在接下來章節的範例中,我們假設
env
單純只以
loader=jinja2.
FileSystemLoader('/path/to/templates')
來實體化,而沒有進一步強化 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival

Publisher Resources

ISBN: 9789864766819