默認下,下面的函數在全局作用域中可用:
range([*start*],?*stop*[,?*step*])[](http://docs.jinkan.org/docs/jinja2/templates.html#range "Permalink to this definition")
返回一個包含整等差級數的列表。 range(i, j) 返回 [i, i+1, i+2, ...., j-1] ;起始值(!)默認為 0 。當給定了公差,它決定了增長(或減小)。 例如 range(4) 返回 [0, 1, 2, 3] 。末端的值被丟棄了。這些是一個 4 元素 數組的有效索引值。
例如重復一個模板塊多次來填充一個列表是有用的。想向你有一個 7 個用戶的 列表,但你想要渲染三個空項目來用 CSS 強制指定高度:
~~~
<ul>
{% for user in users %}
<li>{{ user.username }}</li>
{% endfor %}
{% for number in range(10 - users|count) %}
<li class="empty"><span>...</span></li>
{% endfor %}
</ul>
~~~
lipsum(*n=5*,?*html=True*,?*min=20*,?*max=100*)[](http://docs.jinkan.org/docs/jinja2/templates.html#lipsum "Permalink to this definition")
在模板中生成 lorem ipsum 亂數假文。默認會生成 5 段 HTML ,每段在 20 到 100 詞之間。如果 HTML 被禁用,會返回常規文本。這在測試布局時生成簡單內容時很有 用。
dict(***items*)[](http://docs.jinkan.org/docs/jinja2/templates.html#dict "Permalink to this definition")
方便的字典字面量替代品。?{'foo'?:?'bar'}?與?dict(foo=bar)?等價。
*class?*cycler(**items*)[](http://docs.jinkan.org/docs/jinja2/templates.html#cycler "Permalink to this definition")
周期計允許你在若干個值中循環,類似?loop.cycle?的工作方式。不同于?loop.cycle?的是,無論如何你都可以在循環外或在多重循環中使用它。
比如如果你想要顯示一個文件夾和文件列表,且文件夾在上,它們在同一個列表中且 行顏色是交替的。
下面的例子展示了如何使用周期計:
~~~
{% set row_class = cycler('odd', 'even') %}
<ul class="browser">
{% for folder in folders %}
<li class="folder {{ row_class.next() }}">{{ folder|e }}</li>
{% endfor %}
{% for filename in files %}
<li class="file {{ row_class.next() }}">{{ filename|e }}</li>
{% endfor %}
</ul>
周期計有下面的屬性和方法:
~~~
reset()[](http://docs.jinkan.org/docs/jinja2/templates.html#cycler.reset "Permalink to this definition")
重置周期計到第一個項。
next()[](http://docs.jinkan.org/docs/jinja2/templates.html#cycler.next "Permalink to this definition")
返回當前項并跳轉到下一個。
current[](http://docs.jinkan.org/docs/jinja2/templates.html#cycler.current "Permalink to this definition")
返回當前項。.
New in version 2.1.
*class?*joiner(*sep='*,?*'*)[](http://docs.jinkan.org/docs/jinja2/templates.html#joiner "Permalink to this definition")
一個小巧的輔助函數用于“連接”多個節。連接器接受一個字符串,每次被調用時返回 那個字符串,除了第一次調用時返回一個空字符串。你可以使用它來連接:
~~~
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
<a href="?action=edit">Edit</a>
{% endif %}
~~~
New in version 2.1.
- 介紹
- 預備知識
- 安裝
- 基本 API 使用
- 實驗性的 Python 3 支持
- API
- 基礎
- Unicode
- 高層 API
- 自動轉義
- 標識符的說明
- 未定義類型
- 上下文
- 加載器
- 字節碼緩存
- 實用工具
- 異常
- 自定義過濾器
- 求值上下文
- 自定義測試
- 全局命名空間
- 低層 API
- 元 API
- 沙箱
- API
- 運算符攔截
- 模板設計者文檔
- 概要
- 變量
- 過濾器
- 測試
- 注釋
- 空白控制
- 轉義
- 行語句
- 模板繼承
- HTML 轉義
- 控制結構清單
- 導入上下文行為
- 表達式
- 內置過濾器清單
- 內置測試清單
- 全局函數清單
- 擴展
- 自動轉義擴展
- 擴展
- 添加擴展
- i18n 擴展
- 表達式語句
- 循環控制
- With 語句
- 自動轉義擴展
- 編寫擴展
- 集成
- Babel 集成
- Pylons
- TextMate
- Vim
- 從其它的模板引擎切換
- Jinja1
- Django
- Mako
- 提示和技巧
- Null-Master 退回
- 交替的行
- 高亮活動菜單項
- 訪問父級循環