http://docs.ckan.org/en/2.9/theming/templates.html#template-snippets
Template snippets 是一小段/snippet template code,如同 helper functions,可在 template 裡呼叫。CKAN 在 ckan/templates/ 目錄下,可看到如:ckan/templates/snippets/ 及 ckan/templates/package/snippets/。
如下 ckan/templates/group/snippets/group_list.html 這個 snippet,可協助很好地顯示 group 清單。它裡面又呼叫了另一段 snippet- group/snippets/group_item.html:
{#
Display a grid of group items.
groups - A list of groups.
Example:
{% snippet "group/snippets/group_list.html" %}
#}
{% block group_list %}
<ul class="media-grid" data-module="media-grid">
{% block group_list_inner %}
{% for group in groups %}
{% snippet "group/snippets/group_item.html", group=group, position=loop.index %}
{% endfor %}
{% endblock %}
</ul>
{% endblock %}
<ul class="media-grid" data-module="media-grid">
{% block group_list_inner %}
{% for group in groups %}
{% snippet "group/snippets/group_item.html", group=group, position=loop.index %}
{% endfor %}
{% endblock %}
</ul>
{% endblock %}
修改 layout1.html
ckanext-example_theme/ckanext/example_theme/templates/home/layout1.html:
⋯⋯略
{% block featured_organization %}
<h3>熱門群組</h3>
{# Call the group_list.html snippet. #}
{% snippet 'group/snippets/group_list.html',
groups=h.example_theme_most_popular_groups() %}
{% endblock %}
如上程式,傳了兩個參數給 {% snippet %} tag:
- 'group/snippets/group_list.html'
要呼叫的 template snippet - groups=h.example_theme_most_popular_groups()
在 snippet 檔名後面,可傳入任意個參數給 snippet 裡的程式,變成 snippet 裡的全域變數。每個 snippet 檔應該在最前面的 docstring 註解它需要傳入那些參數。
更新後,首頁右下區塊結果如下:
沒有留言:
張貼留言