Commit cdf5e553 authored by Zenon Mousmoulas's avatar Zenon Mousmoulas

Fix template tags include_maybe, template_exists

parent dcfcddae
from django import template
from django.template.defaultfilters import stringfilter
from django.template.loader_tags import do_include
from django.template.defaulttags import CommentNode
register = template.Library()
@register.tag('include_maybe')
def do_include_maybe(parser, token):
# Source: http://stackoverflow.com/a/18951166/15690
bits = token.split_contents()
if len(bits) < 2:
raise template.TemplateSyntaxError(
"%r tag takes at least one argument: "
"the name of the template to be included." % bits[0])
@register.filter
@stringfilter
def template_exists(value):
try:
template.loader.get_template(value)
return True
silent_node = do_include(parser, token)
except template.TemplateDoesNotExist:
return False
# Django < 1.7
return CommentNode()
_orig_render = silent_node.render
def wrapped_render(*args, **kwargs):
try:
return _orig_render(*args, **kwargs)
except template.TemplateDoesNotExist:
return CommentNode()
silent_node.render = wrapped_render
return silent_node
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def template_exists(value):
try:
template.loader.get_template(value)
return True
except template.TemplateDoesNotExist:
return False
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment