New in version 2.2.
元 API 返回一些關于抽象語法樹的信息,這些信息能幫助應用實現更多的高級模板概 念。所有的元 API 函數操作一個?[Environment.parse()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment.parse "jinja2.Environment.parse")?方法返回的抽象語法 樹。
jinja2.meta.find_undeclared_variables(*ast*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.meta.find_undeclared_variables "Permalink to this definition")
Returns a set of all variables in the AST that will be looked up from the context at runtime. Because at compile time it’s not known which variables will be used depending on the path the execution takes at runtime, all variables are returned.
~~~
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')
>>> meta.find_undeclared_variables(ast)
set(['bar'])
~~~
Implementation
Internally the code generator is used for finding undeclared variables. This is good to know because the code generator might raise a?[TemplateAssertionError](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.TemplateAssertionError "jinja2.TemplateAssertionError")?during compilation and as a matter of fact this function can currently raise that exception as well.
jinja2.meta.find_referenced_templates(*ast*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.meta.find_referenced_templates "Permalink to this definition")
Finds all the referenced templates from the AST. This will return an iterator over all the hardcoded template extensions, inclusions and imports. If dynamic inheritance or inclusion is used,?None?will be yielded.
~~~
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% extends "layout.html" %}{% include helper %}')
>>> list(meta.find_referenced_templates(ast))
['layout.html', None]
~~~
This function is useful for dependency tracking. For example if you want to rebuild parts of the website after a layout template has changed.
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環