[Fixed]-If..else custom template tag

19👍

You can use template filters inside if statements. So you could rewrite your tag as a filter:

{% if request.user|check_can_edit:article %}

Note that it’s tricky to pass multiple arguments of different types to a filter, so you’ll probably want to use one filter per permission, above I’ve used check_can_edit.

18👍

You can definitely do that if you’re willing to write some more lines of python code to improve your template readability! 🙂

You need to parse the tag content yourself, even the parameters it takes and then resolve them, if you want to use variables on them.

The tag implemented below can be used like this:

{% load mytag %}
{% mytag True %}Hi{% else %}Hey{% endmytag %} Bro

Or with a variable:

{% mytag myobject.myflag %}Hi{% else %}Hey{% endmytag %} Bro

So, here’s the way I did it:

from django.template import Library, Node, TemplateSyntaxError

register = Library()

@register.tag
def mytag(parser, token):
    # Separating the tag name from the "test" parameter.
    try:
        tag, test = token.contents.split()
    except (ValueError, TypeError):
        raise TemplateSyntaxError(
            "'%s' tag takes two parameters" % tag)

    default_states = ['mytag', 'else']
    end_tag = 'endmytag'

    # Place to store the states and their values
    states = {}

    # Let's iterate over our context and find our tokens
    while token.contents != end_tag:
        current = token.contents
        states[current.split()[0]] = parser.parse(default_states + [end_tag])
        token = parser.next_token()

    test_var = parser.compile_filter(test)
    return MyNode(states, test_var)


class MyNode(Node):
    def __init__(self, states, test_var):
        self.states = states
        self.test_var = test_var

    def render(self, context):
        # Resolving variables passed by the user
        test_var = self.test_name.resolve(context, True)

        # Rendering the right state. You can add a function call, use a
        # library or whatever here to decide if the value is true or false.
        is_true = bool(test_var)
        return self.states[is_true and 'myvar' or 'else'].render(context)

And that’s it. HTH.

2👍

In Django 2 the assignment tag was replaced by simple_tag() but you could store the custom tag result as a template variable:

# I'm assuming that check_permission receives user and article,
# checks if the user can edit the article and return True or False
{% check_permission user article as permission_cleared %}
{% if permission_cleared %}
    <form>...</form>
{% else %}
    {{ article }}
{% endif %}

Check the current doc about custom template tags: https://docs.djangoproject.com/en/2.1/howto/custom-template-tags/#simple-tags

1👍

inside my_tags.py

from django import template
register = template.Library()

@register.simple_tag(takes_context=True)
def make_my_variable_true(context):
    context['my_variable'] = True
    return ''  # without this you'll get a "None" in your html

inside my_template.html

{% load my_tags %}
{% make_my_variable_true %}
{% if my_variable %}foo{% endif %}

0👍

In this case best solution is to use custom filter. If you don’t want write long code for custom tag. Also if you don’t want to copy/paste others code.
Here is an example

Inside templatetag

register = template.Library()
def exam_available(user, skill):
    skill = get_object_or_404(Skill, id=skill)
    return skill.exam_available(user)
register.filter('exam_available', exam_available)

Inside template

    {{ request.user|exam:skill.id  }}
    or
    {% if request.user|exam:skill.id  %}

Since one of the main common of it is to use request.user or any specific object(id) inside model’s custom method, so filtering that individual object or user is the easiest way to make it done. 🙂

Leave a comment