Ticket #9093: inclusion_tag_using_template.diff
File inclusion_tag_using_template.diff, 5.1 KB (added by , 16 years ago) |
---|
-
django/template/__init__.py
823 823 raise TemplateSyntaxError(message) 824 824 return node_class(bits) 825 825 826 def inclusion_tag_compiler(params, defaults, name, node_class, parser, token): 827 "Returns an InclusionNode." 828 bits = token.split_contents()[1:] 829 using_template = [] # Allow multiple templates via select_template. 830 try: 831 using_index = bits.index('using') 832 except ValueError: 833 pass 834 else: 835 if using_index != len(bits) - 1: 836 # Get everything after the 'using' keyword. 837 using_template[:] = bits[using_index + 1:] 838 bits = bits[:using_index] 839 return node_class(bits, using_template) 840 826 841 class Library(object): 827 842 def __init__(self): 828 843 self.filters = {} … … 886 901 def render(self, context): 887 902 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 888 903 return func(*resolved_vars) 889 904 890 905 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode) 891 906 compile_func.__doc__ = func.__doc__ 892 907 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 893 908 return func 894 909 895 def inclusion_tag(self, file_name, context_class=Context, takes_context=False ):910 def inclusion_tag(self, file_name, context_class=Context, takes_context=False, allow_template_override=True): 896 911 def dec(func): 897 912 params, xx, xxx, defaults = getargspec(func) 898 913 if takes_context: … … 900 915 params = params[1:] 901 916 else: 902 917 raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'") 918 919 class InclusionNode(Node): 920 default_template = file_name 903 921 904 class InclusionNode(Node):905 def __init__(self, vars_to_resolve):922 def __init__(self, vars_to_resolve, using_template=()): 923 self.using_template = map(Variable, using_template) 906 924 self.vars_to_resolve = map(Variable, vars_to_resolve) 907 925 908 926 def render(self, context): … … 915 933 dict = func(*args) 916 934 917 935 if not getattr(self, 'nodelist', False): 936 using_template = [var.resolve(context) for var in self.using_template] 937 file_name = using_template or self.default_template 918 938 from django.template.loader import get_template, select_template 919 939 if not isinstance(file_name, basestring) and is_iterable(file_name): 920 940 t = select_template(file_name) … … 923 943 self.nodelist = t.nodelist 924 944 return self.nodelist.render(context_class(dict, 925 945 autoescape=context.autoescape)) 926 927 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) 946 947 if allow_template_override: 948 tag_compiler = inclusion_tag_compiler 949 else: 950 tag_compiler = generic_tag_compiler 951 compile_func = curry(tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) 928 952 compile_func.__doc__ = func.__doc__ 929 953 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 930 954 return func 931 955 return dec 932 956 957 933 958 def get_library(module_name): 934 959 lib = libraries.get(module_name, None) 935 960 if not lib: -
docs/howto/custom-template-tags.txt
706 706 the tag is passed the context object, as in this example. That's the only 707 707 difference between this case and the previous ``inclusion_tag`` example. 708 708 709 Sometimes you may wish to use your inclusion tag with a template other than the 710 one specified in ``register.inclusion_tag()``. Template tags offer the 711 following syntax for this scenario: 712 713 .. code-block:: html+django 714 715 {% show_results poll using "custom/results.html" %} 716 717 This will using the template found at ``custom/results.html`` instead of the 718 default of ``results.html``. Multiple templates may be provided with this 719 syntax -- Django's template loader will select which to use: 720 721 .. code-block:: html+django 722 723 {% show_results poll using "site/results.html" "app/results.html" %} 724 725 To disallow overriding the template for your inclusion tag, specify 726 ``allow_template_override=False`` in ``register.inclusion_tag()``: 727 728 register.inclusion_tag('results.html', allow_template_override=False)(show_results) 729 709 730 Setting a variable in the context 710 731 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 711 732