etc/templatetags/etc_misc.py
def include_(parser, token):
    """Similar to built-in ``include`` template tag, but allowing
    template variables to be used in template name and a fallback template,
    thus making the tag more dynamic.

    .. warning:: Requires Django 1.8+

    Example:

        {% load etc_misc %}
        {% include_ "sub_{{ postfix_var }}.html" fallback "default.html" %}

    """
    bits = token.split_contents()

    dynamic = False

    # We fallback to built-in `include` if a template name contains no variables.
    if len(bits) >= 2:
        dynamic = '{{' in bits[1]

        if dynamic:
            fallback = None
            bits_new = []

            for bit in bits:

                if fallback is True:
                    # This bit is a `fallback` argument.
                    fallback = bit
                    continue

                if bit == 'fallback':
                    fallback = True

                else:
                    bits_new.append(bit)

            if fallback:
                fallback = parser.compile_filter(construct_relative_path_(parser, fallback))

            token.contents = ' '.join(bits_new)

    token.contents = token.contents.replace('include_', 'include')
    include_node = do_include(parser, token)

    if dynamic:
        # swap simple include with dynamic
        include_node = DynamicIncludeNode(
            include_node.template,
            extra_context=include_node.extra_context,
            isolated_context=include_node.isolated_context,
            fallback=fallback or None,
        )

    return include_node
def include_(parser, token):
    bits = token.split_contents()

    dynamic = False

    # We fallback to built-in `include` if a template name contains no variables.
    if len(bits) >= 2:
        dynamic = '{{' in bits[1]

        if dynamic:
            fallback = None
            bits_new = []

            for bit in bits:

                if fallback is True:
                    # This bit is a `fallback` argument.
                    fallback = bit
                    continue

                if bit == 'fallback':
                    fallback = True

                else:
                    bits_new.append(bit)

            if fallback:
                fallback = parser.compile_filter(construct_relative_path_(parser, fallback))

            token.contents = ' '.join(bits_new)

    token.contents = token.contents.replace('include_', 'include')
    include_node = do_include(parser, token)

    if dynamic:
        # swap simple include with dynamic
        include_node = DynamicIncludeNode(
            include_node.template,
            extra_context=include_node.extra_context,
            isolated_context=include_node.isolated_context,
            fallback=fallback or None,
        )

    return include_node
def include_ ( parser , token ) : bits = token . split_contents ( ) dynamic = False # We fallback to built-in `include` if a template name contains no variables. if len ( bits ) >= 2 : dynamic = '{{' in bits [ 1 ] if dynamic : fallback = None bits_new = [ ] for bit in bits : if fallback is True : # This bit is a `fallback` argument. fallback = bit continue if bit == 'fallback' : fallback = True else : bits_new . append ( bit ) if fallback : fallback = parser . compile_filter ( construct_relative_path_ ( parser , fallback ) ) token . contents = ' ' . join ( bits_new ) token . contents = token . contents . replace ( 'include_' , 'include' ) include_node = do_include ( parser , token ) if dynamic : # swap simple include with dynamic include_node = DynamicIncludeNode ( include_node . template , extra_context = include_node . extra_context , isolated_context = include_node . isolated_context , fallback = fallback or None , ) return include_node
Similar to built-in ``include`` template tag, but allowing
    template variables to be used in template name and a fallback template,
    thus making the tag more dynamic.

    .. warning:: Requires Django 1.8+

    Example:

        {% load etc_misc %}
        {% include_ "sub_{{ postfix_var }}.html" fallback "default.html" %}
Similar to built - in include template tag but allowing template variables to be used in template name and a fallback template thus making the tag more dynamic .