a filter that uses a decorator marksafe return div datanamesdiv value register filter def noopvalue paramnone expected noparams doc return noparams expected result noparams anything expected noparams dict register simpletag def oneparamarg expected explicitnocontext doc return explicitnocontext expected result s arg explicitnocontext anything expected explicitnocontext dict register simpletagtakescontexttrue def noparamswithcontextcontext expected paramsandcontext doc return paramsandcontext expected result context value s s contextvalue arg paramsandcontext anything expected paramsandcontext dict register simpletag def simpletwoparamsone two expected simpleonedefault doc return simpleonedefault expected result s s one two simpleonedefault anything expected simpleonedefault dict register simpletag def simpleunlimitedargsone twohi args expected simpleonlyunlimitedargs doc return simpleonlyunlimitedargs expected result s join strarg for arg in args simpleonlyunlimitedargs anything expected simpleonlyunlimitedargs dict register simpletag def simpleunlimitedargskwargsone twohi args kwargs expected simpletagwithoutcontextparameter doc return expected result simpletagwithoutcontextparameter anything expected simpletagwithoutcontextparameter dict register simpletagtakescontexttrue def simpletagtakescontextwithoutparams a tag that doesn t even think about escaping issues return hello formatcontextname register simpletagtakescontexttrue def escapeexplicitcontext a tag that uses formathtml return formathtmlhello 0 contextname register simpletagtakescontexttrue def currentappcontext return strcontext currentapp register simpletagtakescontexttrue def usel10ncontext return strcontext usel10n register simpletagnameminustwo def minustwooverriddennamevalue return value 2 register simpletaglambda x x 1 nameminusone register tagcounter def counterparser token return counternode class counternodetemplate node def initself self count 0 def renderself context count self count self count count 1 return strcount register tagextradata def doextradataparser token parser extradataextradata customdata return textnode a filter that uses a decorator mark_safe a noop filter that always return its first argument and does nothing with its second optional one useful for testing out whitespace in filter arguments see 19882 expected no_params __doc__ expected one_param __doc__ expected explicit_no_context __doc__ expected no_params_with_context __doc__ expected params_and_context __doc__ expected simple_two_params __doc__ expected simple_one_default __doc__ expected simple_unlimited_args __doc__ expected simple_only_unlimited_args __doc__ expected simple_unlimited_args_kwargs __doc__ expected simple_tag_without_context_parameter __doc__ expected simple_tag_takes_context_without_params __doc__ a tag that doesn t even think about escaping issues a tag that uses escape explicitly a tag that uses format_html
from django import template
from django.template.base import TextNode
from django.template.defaultfilters import stringfilter
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe

register = template.Library()


@register.filter
@stringfilter
def trim(value, num):
    return value[:num]


@register.filter
@mark_safe
def make_data_div(value):
    
    return '<div data-name="%s"></div>' % value


@register.filter
def noop(value, param=None):
    
    return value


@register.simple_tag(takes_context=True)
def context_stack_length(context):
    return len(context.dicts)


@register.simple_tag
def no_params():
    
    return "no_params - Expected result"


no_params.anything = "Expected no_params __dict__"


@register.simple_tag
def one_param(arg):
    
    return "one_param - Expected result: %s" % arg


one_param.anything = "Expected one_param __dict__"


@register.simple_tag(takes_context=False)
def explicit_no_context(arg):
    
    return "explicit_no_context - Expected result: %s" % arg


explicit_no_context.anything = "Expected explicit_no_context __dict__"


@register.simple_tag(takes_context=True)
def no_params_with_context(context):
    
    return (
        "no_params_with_context - Expected result (context value: %s)"
        % context["value"]
    )


no_params_with_context.anything = "Expected no_params_with_context __dict__"


@register.simple_tag(takes_context=True)
def params_and_context(context, arg):
    
    return "params_and_context - Expected result (context value: %s): %s" % (
        context["value"],
        arg,
    )


params_and_context.anything = "Expected params_and_context __dict__"


@register.simple_tag
def simple_two_params(one, two):
    
    return "simple_two_params - Expected result: %s, %s" % (one, two)


simple_two_params.anything = "Expected simple_two_params __dict__"


@register.simple_tag
def simple_keyword_only_param(*, kwarg):
    return "simple_keyword_only_param - Expected result: %s" % kwarg


@register.simple_tag
def simple_keyword_only_default(*, kwarg=42):
    return "simple_keyword_only_default - Expected result: %s" % kwarg


@register.simple_tag
def simple_one_default(one, two="hi"):
    
    return "simple_one_default - Expected result: %s, %s" % (one, two)


simple_one_default.anything = "Expected simple_one_default __dict__"


@register.simple_tag
def simple_unlimited_args(one, two="hi", *args):
    
    return "simple_unlimited_args - Expected result: %s" % (
        ", ".join(str(arg) for arg in [one, two, *args])
    )


simple_unlimited_args.anything = "Expected simple_unlimited_args __dict__"


@register.simple_tag
def simple_only_unlimited_args(*args):
    
    return "simple_only_unlimited_args - Expected result: %s" % ", ".join(
        str(arg) for arg in args
    )


simple_only_unlimited_args.anything = "Expected simple_only_unlimited_args __dict__"


@register.simple_tag
def simple_unlimited_args_kwargs(one, two="hi", *args, **kwargs):
    
    return "simple_unlimited_args_kwargs - Expected result: %s / %s" % (
        ", ".join(str(arg) for arg in [one, two, *args]),
        ", ".join("%s=%s" % (k, v) for (k, v) in kwargs.items()),
    )


simple_unlimited_args_kwargs.anything = "Expected simple_unlimited_args_kwargs __dict__"


@register.simple_tag(takes_context=True)
def simple_tag_without_context_parameter(arg):
    
    return "Expected result"


simple_tag_without_context_parameter.anything = (
    "Expected simple_tag_without_context_parameter __dict__"
)


@register.simple_tag(takes_context=True)
def simple_tag_takes_context_without_params():
    
    return "Expected result"


simple_tag_takes_context_without_params.anything = (
    "Expected simple_tag_takes_context_without_params __dict__"
)


@register.simple_tag(takes_context=True)
def escape_naive(context):
    
    return "Hello {}!".format(context["name"])


@register.simple_tag(takes_context=True)
def escape_explicit(context):
    
    return escape("Hello {}!".format(context["name"]))


@register.simple_tag(takes_context=True)
def escape_format_html(context):
    
    return format_html("Hello {0}!", context["name"])


@register.simple_tag(takes_context=True)
def current_app(context):
    return str(context.current_app)


@register.simple_tag(takes_context=True)
def use_l10n(context):
    return str(context.use_l10n)


@register.simple_tag(name="minustwo")
def minustwo_overridden_name(value):
    return value - 2


register.simple_tag(lambda x: x - 1, name="minusone")


@register.tag("counter")
def counter(parser, token):
    return CounterNode()


class CounterNode(template.Node):
    def __init__(self):
        self.count = 0

    def render(self, context):
        count = self.count
        self.count = count + 1
        return str(count)


@register.tag("extra_data")
def do_extra_data(parser, token):
    parser.extra_data["extra_data"] = "CUSTOM_DATA"
    return TextNode("")