2011-05-03 39 views
1

我正在使用自定义模板标签,它调用另一个标签,具体取决于它是哪个小时。调用另一个使用@ register.inclusion_tag的模板标签

@register.simple_tag(takes_context=True) 
def my_custom_template_tag(context): 
""" 
""" 
now = datetime.now() 

# if the current hour:minute is less than 
# the publication switch settings defined hour 
if now.strftime('%H:%M') <= settings.PUBLICATION_SWITCH_TIME: 
    print now.strftime('%H:%M') 
    return my_other_template_tag(context) 
else: 
    pass 

@register.inclusion_tag('my_other_template_tag_template_path', takes_context=True) 
def my_other_template_tag(context): 
""" 

""" 
return { 
    'foo' 
} 

的问题是,my_custom_template_tag似乎忽视了所谓的 “my_other_template_tag” @inclusion_tag。有没有办法做到这一点,同时保持使用@inclusion_tag?

谢谢!

回答

2

我有一个稍微不同的问题,并希望在我的视图中呈现模板标签,所以我写了一个辅助函数,允许我这样做。它的变体也应该用于在simple_tag内进行渲染。这里的帮手:

def render_templatetag(request, tag_string, tag_file, dictionary=None): 
    dictionary = dictionary or {} 
    context_instance = RequestContext(request) 
    context_instance.update(dictionary) 
    t = Template("{%% load %s %%}{%% %s %%}" % (tag_file, tag_string)) 
    return t.render(context_instance) 

它只是创造上加载正确的标记文件动态模板,并有您所使用的template_tag。为了在simple_tag中使用,您可以修改该函数并用上下文替换请求arg。