這些輔助函數和類在你向 Jinja2 環境中添加自定義過濾器或函數時很有用。
jinja2.environmentfilter(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.environmentfilter "Permalink to this definition")
Decorator for marking evironment dependent filters. The current?[Environment](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment "jinja2.Environment")?is passed to the filter as first argument.
jinja2.contextfilter(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfilter "Permalink to this definition")
Decorator for marking context dependent filters. The current?Context?will be passed as first argument.
jinja2.evalcontextfilter(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.evalcontextfilter "Permalink to this definition")
Decorator for marking eval-context dependent filters. An eval context object is passed as first argument. For more information about the eval context, see?[*求值上下文*](http://docs.jinkan.org/docs/jinja2/api.html#eval-context).
New in version 2.4.
jinja2.environmentfunction(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.environmentfunction "Permalink to this definition")
This decorator can be used to mark a function or method as environment callable. This decorator works exactly like the?[contextfunction()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfunction "jinja2.contextfunction")?decorator just that the first argument is the active?[Environment](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Environment "jinja2.Environment")?and not context.
jinja2.contextfunction(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfunction "Permalink to this definition")
This decorator can be used to mark a function or method context callable. A context callable is passed the active?Context?as first argument when called from the template. This is useful if a function wants to get access to the context or functions provided on the context object. For example a function that returns a sorted list of template variables the current template exports could look like this:
~~~
@contextfunction
def get_exported_names(context):
return sorted(context.exported_vars)
~~~
jinja2.evalcontextfunction(*f*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.evalcontextfunction "Permalink to this definition")
This decorator can be used to mark a function or method as an eval context callable. This is similar to the?[contextfunction()](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.contextfunction "jinja2.contextfunction")?but instead of passing the context, an evaluation context object is passed. For more information about the eval context, see[*求值上下文*](http://docs.jinkan.org/docs/jinja2/api.html#eval-context).
New in version 2.4.
jinja2.escape(*s*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.escape "Permalink to this definition")
把字符串?s?中?&?、??、?>?、?'?和?"?轉換為 HTML 安 全的序列。如果你需要在 HTML 中顯示可能包含這些字符的文本,可以使用它。這 個函數不會轉義對象。這個函數不會轉義含有 HTML 表達式比如已轉義數據的對象。
返回值是一個?[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?字符串。
jinja2.clear_caches()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.clear_caches "Permalink to this definition")
Jinja2 keeps internal caches for environments and lexers. These are used so that Jinja2 doesn’t have to recreate environments and lexers all the time. Normally you don’t have to care about that but if you are messuring memory consumption you may want to clean the caches.
jinja2.is_undefined(*obj*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.is_undefined "Permalink to this definition")
Check if the object passed is undefined. This does nothing more than performing an instance check against?[Undefined](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Undefined "jinja2.Undefined")?but looks nicer. This can be used for custom filters or tests that want to react to undefined variables. For example a custom default filter can look like this:
~~~
def default(var, default=''):
if is_undefined(var):
return default
return var
~~~
*class?*jinja2.Markup([*string*])[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "Permalink to this definition")
Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped. This implements the?__html__?interface a couple of frameworks and web applications use.?[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?is a direct subclass of?unicode?and provides all the methods of?unicode?just that it escapes arguments passed and always returnsMarkup.
The?escape?function returns markup objects so that double escaping can’t happen.
The constructor of the?[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?class can be used for three different things: When passed an unicode object it’s assumed to be safe, when passed an object with an HTML representation (has an?__html__?method) that representation is used, otherwise the object passed is converted into a unicode string and then assumed to be safe:
~~~
>>> Markup("Hello <em>World</em>!")
Markup(u'Hello <em>World</em>!')
>>> class Foo(object):
... def __html__(self):
... return '<a href="#">foo</a>'
...
>>> Markup(Foo())
Markup(u'<a href="#">foo</a>')
~~~
If you want object passed being always treated as unsafe you can use the?[escape()](http://docs.jinkan.org/docs/jinja2/templates.html#escape "escape")classmethod to create a?[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?object:
~~~
>>> Markup.escape("Hello <em>World</em>!")
Markup(u'Hello <em>World</em>!')
~~~
Operations on a markup string are markup aware which means that all arguments are passed through the?[escape()](http://docs.jinkan.org/docs/jinja2/templates.html#escape "escape")?function:
~~~
>>> em = Markup("<em>%s</em>")
>>> em % "foo & bar"
Markup(u'<em>foo & bar</em>')
>>> strong = Markup("<strong>%(text)s</strong>")
>>> strong % {'text': '<blink>hacker here</blink>'}
Markup(u'<strong><blink>hacker here</blink></strong>')
>>> Markup("<em>Hello</em> ") + "<foo>"
Markup(u'<em>Hello</em> <foo>')
~~~
*classmethod?*escape(*s*)[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup.escape "Permalink to this definition")
Escape the string. Works like?[escape()](http://docs.jinkan.org/docs/jinja2/templates.html#escape "escape")?with the difference that for subclasses of[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?this function would return the correct subclass.
striptags()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup.striptags "Permalink to this definition")
Unescape markup into an text_type string and strip all tags. This also resolves known HTML4 and XHTML entities. Whitespace is normalized to one:
~~~
>>> Markup("Main » <em>About</em>").striptags()
u'Main \xbb About'
~~~
unescape()[](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup.unescape "Permalink to this definition")
Unescape markup again into an text_type string. This also resolves known HTML4 and XHTML entities:
~~~
>>> Markup("Main » <em>About</em>").unescape()
u'Main \xbb <em>About</em>'
~~~
Note
Jinja2 的?[Markup](http://docs.jinkan.org/docs/jinja2/api.html#jinja2.Markup "jinja2.Markup")?類至少與 Pylons 和 Genshi 兼容。預計不久更多模板 引擎和框架會采用__html__?的概念。
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環