当前位置: 首页>>代码示例>>Python>>正文


Python NodeList.extend方法代码示例

本文整理汇总了Python中django.template.NodeList.extend方法的典型用法代码示例。如果您正苦于以下问题:Python NodeList.extend方法的具体用法?Python NodeList.extend怎么用?Python NodeList.extend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.template.NodeList的用法示例。


在下文中一共展示了NodeList.extend方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: labjs

# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import extend [as 别名]
def labjs(parser, token):
    """
    Renders a labjs queue from linked js.

    Syntax::

        {% labjs %}
        <html of linked JS>
        {% endlabjs %}

    Examples::

    {% labjs %}

        <script
            type="text/javascript"
            src="{{ STATIC_URL }}js/jquery-1.5.2.min.js">
        </script>

        {% wait %}

        <script
            type="text/javascript"
            src="{{ STATIC_URL }}js/jquery.formset.min.js">
        </script>
        <script
            type="text/javascript"
            src="{% url django.views.i18n.javascript_catalog %}">
        </script>

    {% endlabjs %}

    Which would be rendered something like::

     <script type="text/javascript">
        $LAB.queueScript("/static/js/jquery-1.5.2.min.js")
            .queueScript("/static/js/jquery.formset.min.js")
            .queueScript("/jsi18n/");
    </script>

    """
    nodelist = NodeList()
    while True:
        chunk = parser.parse(('endlabjs', 'wait'))
        ptoken = parser.next_token()

        if ptoken.contents == 'wait':
            chunk.append(Wait())
            nodelist.extend(chunk)
        elif ptoken.contents == 'endlabjs':
            nodelist.extend(chunk)
            break

    return LabjsNode(nodelist)
开发者ID:ashwoods,项目名称:django-labjs,代码行数:56,代码来源:labjs.py

示例2: IfBlockNode

# 需要导入模块: from django.template import NodeList [as 别名]
# 或者: from django.template.NodeList import extend [as 别名]
class IfBlockNode(BlockNode):
    def __init__(self, name, nodelist, parent=None, check_node=None):
        self.name, self.nodelist, self.parent = name, nodelist, parent
        self.original_nodelist = NodeList()
        self.final_nodelist = NodeList()
        self.original_nodelist.extend( self.nodelist )
        self.final_nodelist.extend( self.nodelist )
        self.check_node = check_node
        
        self.index = None
        for i, node in enumerate( nodelist ):
            if isinstance(node, self.check_node.__class__):
                if node.filter_expression.token == self.check_node.filter_expression.token:
                    self.index = i
                    break
        
    
    def __repr__(self):
        return "<MyBlock Node: %s. Contents: %r>" % (self.name, self.nodelist)
    
    def render(self, context):
        result = ''
        
        if self.parent:
            p = self
            pre = []
            post = []
            while p.parent:
                p = p.parent
                if p.nodelist == self.original_nodelist:
                    p.nodelist = NodeList()
                else:
                    index = None
                    found = False
                    for i, node in enumerate( p.nodelist ):
                        if isinstance(node, self.check_node.__class__):
                            if node.filter_expression.token == self.check_node.filter_expression.token:
                                index = i
                                found = True
                    if found:
                        pre[0:0] = p.nodelist[:index]
                        post.extend(p.nodelist[index+1:])

                        p.nodelist = NodeList()
            if pre:
                self.final_nodelist[self.index:self.index] = pre
                self.index += len(pre)
            if post:
                self.final_nodelist[self.index+1:self.index+1] = post
            
            context.push()
            # Save context in case of block.super().
            self.context = context
            context['block'] = self
            pre_result = self.nodelist.render(context)
            
            context['blockoutput'] = pre_result
            result = self.final_nodelist.render(context)
            context.pop()
        
        return result
    
    def super(self):
        if self.parent:
            return mark_safe(self.parent.render(self.context))
        return ''
    
    def add_parent(self, nodelist):
        if self.parent:
            self.parent.add_parent(nodelist)
        else:
            self.parent = BlockNode(self.name, nodelist)
开发者ID:hcwebdev,项目名称:conditionalblock,代码行数:74,代码来源:conditionalblock.py


注:本文中的django.template.NodeList.extend方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。