2011-05-03 20 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。

相關問題