Source

fat-po-editor / fat / fat_po_editor / templatetags / js_util.py

Full commit
# -*- coding: utf-8 -*-

from django import template

register = template.Library()

# domready
_domready_stack = []

@register.tag
def domready(parser, token):
    nodelist = parser.parse(('enddomready',))
    parser.delete_first_token()
    return DomreadyNode(nodelist)

class DomreadyNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist
        
    def render(self, context):
        _domready_stack.append(self.nodelist.render(context))
        return ''

@register.tag
def domready_render(parser, token):
    return DomreadyRenderNode()

class DomreadyRenderNode(template.Node):
    def render(self, context):
        global _domready_stack
        
        to_return = u"\n".join(_domready_stack)
        _domready_stack = []
        return to_return

# additional_js
_additional_js_stack = []

@register.tag
def add_js(parser, token):
    nodelist = parser.parse(('end_add_js',))
    parser.delete_first_token()
    return AddJsNode(nodelist)

class AddJsNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist
    
    def render(self, context):
        _additional_js_stack.append(self.nodelist.render(context))
        return ''

@register.tag
def additional_js_render(parser, token):
    return AdditionalJsRenderNode()

class AdditionalJsRenderNode(template.Node):
    def render(self, context):
        global _additional_js_stack
        
        to_return = u"\n".join(_additional_js_stack)
        _additional_js_stack = []
        return to_return