Django

Code

Changeset 1349

Show
Ignore:
Timestamp:
11/21/05 23:44:04 (2 years ago)
Author:
adrian
Message:

Fixed #598 -- Added {% include %} template tag. Added docs and unit tests. Thanks, rjwittams

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/template/loader.py

    r1347 r1349  
    1818 
    1919from django.core.exceptions import ImproperlyConfigured 
    20 from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, register_tag 
     20from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, resolve_variable, register_tag 
    2121from django.conf.settings import TEMPLATE_LOADERS 
    2222 
     
    161161        return compiled_parent.render(context) 
    162162 
     163class ConstantIncludeNode(Node): 
     164    def __init__(self, template_path): 
     165        try: 
     166            t = get_template(template_path) 
     167            self.template = t 
     168        except: 
     169            self.template = None 
     170 
     171    def render(self, context): 
     172        if self.template: 
     173            return self.template.render(context) 
     174        else: 
     175            return '' 
     176 
     177class IncludeNode(Node): 
     178    def __init__(self, template_name): 
     179        self.template_name = template_name 
     180 
     181    def render(self, context): 
     182         try: 
     183             template_name = resolve_variable(self.template_name, context) 
     184             t = get_template(template_name) 
     185             return t.render(context) 
     186         except: 
     187             return '' # Fail silently for invalid included templates. 
     188 
    163189def do_block(parser, token): 
    164190    """ 
     
    203229    return ExtendsNode(nodelist, parent_name, parent_name_var) 
    204230 
     231def do_include(parser, token): 
     232    """ 
     233    Loads a template and renders it with the current context. 
     234 
     235    Example:: 
     236 
     237        {% include "foo/some_include" %} 
     238    """ 
     239    bits = token.contents.split() 
     240    if len(bits) != 2: 
     241        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0] 
     242    path = bits[1] 
     243    if path[0] in ('"', "'") and path[-1] == path[0]: 
     244        return ConstantIncludeNode(path[1:-1]) 
     245    return IncludeNode(bits[1]) 
     246 
    205247register_tag('block', do_block) 
    206248register_tag('extends', do_extends) 
     249register_tag('include', do_include) 
  • django/trunk/docs/templates.txt

    r1343 r1349  
    492492 
    493493Just like ``ifequal``, except it tests that the two arguments are not equal. 
     494 
     495include 
     496~~~~~~~ 
     497 
     498**Only available in Django development version.** 
     499 
     500Loads a template and renders it with the current context. This is a way of 
     501"including" other templates within a template. 
     502 
     503The template name can either be a variable or a hard-coded (quoted) string, 
     504in either single or double quotes. 
     505 
     506This example includes the contents of the template ``"foo/bar"``:: 
     507 
     508    {% include "foo/bar" %} 
     509 
     510This example includes the contents of the template whose name is contained in 
     511the variable ``template_name``:: 
     512 
     513    {% include template_name %} 
     514 
     515An included template is rendered with the context of the template that's 
     516including it. This example produces the output ``"Hello, John"``: 
     517 
     518    * Context: variable ``person`` is set to ``"john"``. 
     519    * Template:: 
     520 
     521        {% include "name_snippet" %} 
     522 
     523    * The ``name_snippet`` template:: 
     524 
     525        Hello, {{ person }} 
     526 
     527See also: ``{% ssi %}``. 
    494528 
    495529load 
     
    646680`ALLOWED_INCLUDE_ROOTS`_ in your Django settings, as a security measure. 
    647681 
     682See also: ``{% include %}``. 
     683 
    648684.. _ALLOWED_INCLUDE_ROOTS: http://www.djangoproject.com/documentation/settings/#allowed-include-roots 
    649685 
  • django/trunk/tests/othertests/templates.py

    r1277 r1349  
    134134    'ifnotequal03': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 
    135135    'ifnotequal04': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 1}, "no"), 
     136 
     137    ### INCLUDE TAG ########################################################### 
     138    'include01': ('{% include "basic-syntax01" %}', {}, "something cool"), 
     139    'include02': ('{% include "basic-syntax02" %}', {'headline': 'Included'}, "Included"), 
     140    'include03': ('{% include template_name %}', {'template_name': 'basic-syntax02', 'headline': 'Included'}, "Included"), 
     141    'include04': ('a{% include "nonexistent" %}b', {}, "ab"), 
    136142 
    137143    ### INHERITANCE ###########################################################