Ticket #9093: inclusion_tag_using_template.diff

File inclusion_tag_using_template.diff, 5.1 KB (added by exogen@…, 18 years ago)

Initial version with with docs

  • django/template/__init__.py

     
    823823        raise TemplateSyntaxError(message)
    824824    return node_class(bits)
    825825
     826
     827
     828
     829
     830
     831
     832
     833
     834
     835
     836
     837
     838
     839
     840
    826841class Library(object):
    827842    def __init__(self):
    828843        self.filters = {}
     
    886901            def render(self, context):
    887902                resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    888903                return func(*resolved_vars)
    889 
     904       
    890905        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
    891906        compile_func.__doc__ = func.__doc__
    892907        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    893908        return func
    894909
    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):
    896911        def dec(func):
    897912            params, xx, xxx, defaults = getargspec(func)
    898913            if takes_context:
     
    900915                    params = params[1:]
    901916                else:
    902917                    raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'")
     918
     919
     920
    903921
    904             class InclusionNode(Node):
    905                 def __init__(self, vars_to_resolve):
     922            ):
     923               
    906924                    self.vars_to_resolve = map(Variable, vars_to_resolve)
    907925
    908926                def render(self, context):
     
    915933                    dict = func(*args)
    916934
    917935                    if not getattr(self, 'nodelist', False):
     936
     937
    918938                        from django.template.loader import get_template, select_template
    919939                        if not isinstance(file_name, basestring) and is_iterable(file_name):
    920940                            t = select_template(file_name)
     
    923943                        self.nodelist = t.nodelist
    924944                    return self.nodelist.render(context_class(dict,
    925945                            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)
    928952            compile_func.__doc__ = func.__doc__
    929953            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    930954            return func
    931955        return dec
    932956
     957
    933958def get_library(module_name):
    934959    lib = libraries.get(module_name, None)
    935960    if not lib:
  • docs/howto/custom-template-tags.txt

     
    706706the tag is passed the context object, as in this example. That's the only
    707707difference between this case and the previous ``inclusion_tag`` example.
    708708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
    709730Setting a variable in the context
    710731~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    711732
Back to Top