Пример #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)
Пример #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: []>]})>",
     )
Пример #3
0
def BlockNode_render(self, context):
    block_context = context.render_context.get(BLOCK_CONTEXT_KEY)
    context.push()
    if block_context is None:
        context['block'] = self
        result = self.nodelist.render(context)
    else:
        push = block = block_context.pop(self.name)
        if block is None:
            block = self
        # Create new block so we can store context without thread-safety issues.
        block = BlockNode(block.name, block.nodelist)
        block.context = context
        context['block'] = block
        result = block.nodelist.render(context)
        if push is not None:
            block_context.push(self.name, push)
    context.pop()

    if (self.name == 'content') and not hasattr(context['request'], '_foundry_blocknode_marker'):
        # What view are we rendering?
        try:
            view_name = resolve(context['request'].META['PATH_INFO']).view_name
        except Resolver404:
            return result

        # Mark to prevent recursion
        setattr(context['request'], '_foundry_blocknode_marker', 1)

        # Find page if any. Import here to prevent circular import.
        from foundry.models import Page, PageView
        # Use first permitted page that has row of required type
        pages = Page.permitted.filter(id__in=[o.page.id for o in PageView.objects.filter(view_name=view_name)])
        for page in pages:
            rows = page.row_set.filter(has_left_or_right_column=True)
            if rows.exists():
                html = render_to_string(
                    'foundry/inclusion_tags/rows.html', {'rows':[rows[0]], 'include_center_marker':1}, context
                )
                return html.replace('_FOUNDRY_BLOCKNODE_PLACEHOLDER', result)

    return result
Пример #4
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)
Пример #5
0
 def render_actual(self, context, name):
     return BlockNode(name, NodeList()).render_annotated(context)
Пример #6
0
 def renderNode(self, block, context):
     block = BlockNode(block.name, block.nodelist)
     block.context = context
     return block.nodelist.render(context).strip()
Пример #7
0
 def analyze_block_render(self, context):
     with measure(('block', self.name)):
         result = BlockNode._render(self, context)
     return result