简体   繁体   中英

Django 1.3 specify the template to use in argument with inclusion_tag

I am currently having 2 templatetags for nearly the same thing:

@register.inclusion_tag('content.html')
def content(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

@register.inclusion_tag('spec.html')
def spec(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

I'd like to merge them as only the template to use change, how could I do that ?

You can do it this way:

def tag(key, lazy=False, params=''):
    return {'key': key, 'lazy': js_bool(lazy), 'params': params}

register.inclusion_tag('content.html', name='content')(tag)
register.inclusion_tag('spec.html', name='spec')(tag)

Python decorators are nothing more than syntactic helpers, you can replace

@decorator
def function():
    pass

by

def function():
    pass
function = decorator(function)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM