赞
踩
django模板把指令放在{% %}中,{{}}中的是在python代码中的变量。
extends 与 include分别是模板的继承和包含关系。
{% include "horizon/common/_page_header.html" %}
horizon/common/_page_header.html文件如下:
- {% load i18n %}
- {% block page_header %}
- <div class='page-header'>
- <h2>{{ title }}</h2>
- </div>
- {% endblock %}
其中block是表示重用的块。
父模板中定义:
- {% block context %}
- 父
- {% endblock %}
子模板中使用:
- {% extends 'index.html' %}
- {% block context %}
- 实现
- {% endblock %}
load用来载入一个过滤器或者tag。
- {% load truncate_filter %}
- {% load branding %}
其中templatetags/truncate_filter.py的实现为:
- from django import template
-
- register = template.Library()
-
- @register.filter("truncate")
- def truncate(value, size):
- if len(value) > size and size > 3:
- return value[0:(size - 3)] + '...'
- else:
- return value[0:size]
其中templatetags/branding.py的实现为:
- from django import template
- from django.conf import settings
- from django.utils.translation import ugettext as _
-
- register = template.Library()
-
- class SiteBrandingNode(template.Node):
- def render(self, context):
- return getattr(settings, "SITE_BRANDING", _("Horizon"))
-
- @register.tag
- def site_branding(parser, token):
- return SiteBrandingNode()
- ...
自定义有两种方式一种是通过register,一种是使用装饰器。
另一中方式为:
register.tag('name', fun)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。