Example #1
0
def do_block(parser, token):
    """
    Define a block that can be overridden by child templates. Adapted for Handlebars
    template syntax. Note that you cannot use template variables in these blocks!
    """

    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError("'%s' tag takes only one argument" % bits[0])
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        if block_name in parser.__loaded_blocks:
            raise TemplateSyntaxError(
                "'%s' tag with name '%s' appears more than once" %
                (bits[0], block_name))
        parser.__loaded_blocks.append(block_name)
    except AttributeError:  # parser.__loaded_blocks isn't a list yet
        parser.__loaded_blocks = [block_name]

    acceptable_endblocks = ('endblock_verbatim',
                            'endblock_verbatim %s' % block_name)

    # modify nodelist!
    nodelist = verbatim_tags(parser, token, endtagnames=acceptable_endblocks)

    return BlockNode(block_name, nodelist)
Example #2
0
 def test_repr(self):
     block_context = BlockContext()
     block_context.add_blocks({"content": BlockNode("content", [])})
     self.assertEqual(
         repr(block_context),
         "<BlockContext: blocks=defaultdict(<class 'list'>, "
         "{'content': [<Block Node: content. Contents: []>]})>",
     )
Example #3
0
def do_block(parser, token):
    """Shortcut alias for {% block %} tag."""

    bits = token.contents.split()
    if len(bits) != 2:
        raise TemplateSyntaxError, "'%s' tag takes only one argument" % bits[0]
    block_name = bits[1]
    # Keep track of the names of BlockNodes found in this template, so we can
    # check for duplication.
    try:
        if block_name in parser.__loaded_blocks:
            raise TemplateSyntaxError, "'%s' tag with name '%s' appears more than once" % (
                bits[0], block_name)
        parser.__loaded_blocks.append(block_name)
    except AttributeError:  # parser.__loaded_blocks isn't a list yet
        parser.__loaded_blocks = [block_name]
    nodelist = parser.parse(('endb', 'endb %s' % block_name))
    parser.delete_first_token()
    return BlockNode(block_name, nodelist)
Example #4
0
 def render_actual(self, context, name):
     return BlockNode(name, NodeList()).render_annotated(context)