*class?*jinja2.runtime.Context[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.runtime.Context "Permalink to this definition")
The template context holds the variables of a template. It stores the values passed to the template and also the names the template exports. Creating instances is neither supported nor useful as it’s created automatically at various stages of the template evaluation and should not be created by hand.
The context is immutable. Modifications on?[parent](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.parent "jinja2.Context.parent")?**must not**?happen and modifications on?[vars](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.vars "jinja2.Context.vars")?are allowed from generated template code only. Template filters and global functions marked as?[contextfunction()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfunction "jinja2.contextfunction")s get the active context passed as first argument and are allowed to access the context read-only.
The template context supports read only dict operations (get,?keys,?values,?items,iterkeys,?itervalues,?iteritems,?__getitem__,?__contains__). Additionally there is aresolve()?method that doesn’t fail with a?KeyError?but returns an?[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?object for missing variables.
parent[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.parent "Permalink to this definition")
一個模板查找的只讀全局變量的詞典。這些變量可能來自另一個?Context?,或是[Environment.globals](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment.globals "jinja2.Environment.globals")?,或是?[Template.globals](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Template.globals "jinja2.Template.globals")?,或指向一個由全局變量和傳遞到渲染函數的變 量聯立的字典。它一定不能被修改。
vars[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.vars "Permalink to this definition")
模板局域變量。這個列表包含環境和來自?[parent](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.parent "jinja2.Context.parent")?范圍的上下文函數 以及局域修改和從模板中導出的變量。模板會在模板求值時修改這個字典, 但過濾器和上下文函數不允許修改它。
environment[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.environment "Permalink to this definition")
加載該模板的環境
exported_vars[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.exported_vars "Permalink to this definition")
這設定了所有模板導出量的名稱。名稱對應的值在?[vars](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.vars "jinja2.Context.vars")?字典中。 可以用get_exported()?獲取一份導出變量的拷貝字典。
name[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.name "Permalink to this definition")
擁有此上下文的模板的載入名。
blocks[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.blocks "Permalink to this definition")
模板中塊當前映射的字典。字典中的鍵是塊名稱,值是注冊的塊的列表。每個 列表的最后一項是當前活動的塊(繼承鏈中最新的)。
eval_ctx[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Context.eval_ctx "Permalink to this definition")
當前的?[*求值上下文*](http://docs.jinkan.org/docs/jinja2/api.html#eval-context)?。
call(*callable*,?**args*,?***kwargs*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.runtime.Context.call "Permalink to this definition")
Call the callable with the arguments and keyword arguments provided but inject the active context or environment as first argument if the callable is a[contextfunction()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfunction "jinja2.contextfunction")?or?[environmentfunction()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.environmentfunction "jinja2.environmentfunction").
get_all()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.runtime.Context.get_all "Permalink to this definition")
Return a copy of the complete context as dict including the exported variables.
get_exported()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.runtime.Context.get_exported "Permalink to this definition")
Get a new dict with the exported variables.
resolve(*key*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.runtime.Context.resolve "Permalink to this definition")
Looks up a variable like?__getitem__?or?get?but returns an?[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?object with the name of the name looked up.
實現
Python frame 中的局域變量在函數中是不可變的,出于同樣的原因,上下文是不可 變的。 Jinja2 和 Python 都不把上下文/ frame 作為變量的數據存儲,而只作為 主要的數據源。
當模板訪問一個模板中沒有定義的變量時, Jinja2 在上下文中查找變量,此后, 這個變量被視為其是在模板中定義得一樣。
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環