Tôi đã phải làm một cái gì đó như thế này cho một dự án và kể từ khi chúng tôi cần nhiều hơn một loại thẻ bao gồm tôi đã thực hiện một trang trí dựa trên trang trí django inclusion_tag. Đây là mã:
# -*- coding: utf-8 -*-
from django import template
from inspect import getargspec
from django.template.context import Context
from django.template import Node, generic_tag_compiler, Variable
from django.utils.functional import curry
def inclusion_tag(register, context_class=Context, takes_context=False):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if takes_context:
if params[0] == 'context':
params = params[1:]
else:
raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'")
class InclusionNode(Node):
def __init__(self, vars_to_resolve):
self.vars_to_resolve = map(Variable, vars_to_resolve)
def render(self, context):
resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
if takes_context:
args = [context] + resolved_vars
else:
args = resolved_vars
file_name, extra_context = func(*args)
from django.template.loader import get_template, select_template
if not isinstance(file_name, basestring) and is_iterable(file_name):
t = select_template(file_name)
else:
t = get_template(file_name)
self.nodelist = t.nodelist
new_context = context_class(extra_context, autoescape=context.autoescape)
# Copy across the CSRF token, if present, because inclusion
# tags are often used for forms, and we need instructions
# for using CSRF protection to be as simple as possible.
csrf_token = context.get('csrf_token', None)
if csrf_token is not None:
new_context['csrf_token'] = csrf_token
return self.nodelist.render(new_context)
compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
compile_func.__doc__ = func.__doc__
register.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
return func
return dec
Bạn phải trả lại một bộ dữ liệu với mẫu (hoặc danh sách mẫu) và ngữ cảnh ngữ cảnh. Lưu ý rằng bạn phải vượt qua thanh ghi (Thư viện chẳng hạn) trong các cuộc gọi trang trí:
from somewhere import inclusion_tag
@inclusion_tag(register)
def display_formset(formset):
template_name = FORMSET_TEMPLATES.get(formset.model,
'includes/inline_formset.html')
return (template_name, {'formset': formset})
Hope this helps
Bạn đang cố gắng đạt được điều đó '{% bao gồm my_template%}' có thể không? –
Tôi cần thêm một số biến vào ngữ cảnh bằng thẻ tùy chỉnh. Có vẻ như tôi sẽ phải viết thẻ theo cách dài. Cảm ơn. – meppum