from django import template
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.contrib.staticfiles.storage import staticfiles_storage
register = template.Library()

class IfStaticNode(template.Node):
    child_nodelists = ('nodelist_true', 'nodelist_false')
    def __init__(self, var1, nodelist_true, nodelist_false, negate):
        self.var1 = var1
        self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false
        self.negate = negate

    def __repr__(self):
        return "<IfStaticNode>"

    def render(self, context):
        context.push()
        val1 = self.var1.resolve(context, True)
        context['static'] = static(val1)
        print context['static']

        print val1
        if staticfiles_storage.exists(val1):
            x = self.nodelist_true.render(context)
        else:
            x = self.nodelist_false.render(context)
        context.pop()
        return x

def do_ifstatic(parser, token, negate):
    bits = list(token.split_contents())
    if len(bits) != 2:
        raise TemplateSyntaxError("%r takes one argument" % bits[0])
    end_tag = 'end' + bits[0]
    nodelist_true = parser.parse(('else', end_tag))
    token = parser.next_token()
    if token.contents == 'else':
        nodelist_false = parser.parse((end_tag,))
        parser.delete_first_token()
    else:
        nodelist_false = NodeList()
    val1 = parser.compile_filter(bits[1])
    return IfStaticNode(val1, nodelist_true, nodelist_false, negate)

@register.tag
def ifstatic(parser, token):
    """
    Outputs the contents of the block if the static file exists

    Examples::

        {% ifstatic 'static.ext' %}
            ...
            {{static}}
        {% else %}
        {% endifstatic %}
    """
    return do_ifstatic(parser, token, False)

@@ -579,31 +579,8 @@
 t):%0A
-        context.push()%0A
     
@@ -635,40 +635,44 @@
     
-context%5B'static'%5D = static
+if staticfiles_storage.exists
 (val1)
+:
 %0A   
@@ -680,21 +680,19 @@
     
-print
+   
  context
 %5B'st
@@ -691,83 +691,60 @@
 text
-%5B'static'%5D%0A%0A        print val1%0A        if staticfiles_storage.exists
+.push()%0A            context%5B'static'%5D = static
 (val1)
-:
 %0A   
@@ -783,32 +783,58 @@
 render(context)%0A
+            context.pop()%0A
         else:%0A  
@@ -883,38 +883,16 @@
 ontext)%0A
-        context.pop()%0A
         

improve ifstatic tag