• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

Django里面的自定义tag和filter

Django的文档里面有这么一句

 


The app that contains the custom tags must be in INSTALLED_APPS 
in order for the {% load %} tag to work. 

 

当第一次load一个template的时候,调用源码

 

def get_templatetags_modules():
    """
    Return the list of all available template tag modules.

    Caches the result for faster access.
    """
    global templatetags_modules
    if not templatetags_modules:
        _templatetags_modules = []
        # Populate list once per process. Mutate the local list first, and
        # then assign it to the global name to ensure there are no cases where
        # two threads try to populate it simultaneously.
        for app_module in ['django'] + list(settings.INSTALLED_APPS):
            try:
                templatetag_module = '%s.templatetags' % app_module
                import_module(templatetag_module)
                _templatetags_modules.append(templatetag_module)
            except ImportError:
                continue
        templatetags_modules = _templatetags_modules
    return templatetags_modules


Load类型的Node 输出的定义

 

 

class LoadNode(Node):
    def render(self, context):
        return ''

可见他是一个空白字符串,也就是在输出的html中"抹掉"了

 


example

 

<html>
<head>
	{% load blog_extras %}
	Hi my double value is <b>{{myvalue|mycut}}</b>
</head>
</html>


will be tokenized as

 



Then the block token will be set to LoadNode, var token to Variable Node  in my case, done byParser

最后把每一个node render出来

 

posted @ 2013-09-15 20:33  Class Xman  阅读(211)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3