Mathieu Dhondt
Mathieu Dhondt

Reputation: 8914

if..else custom template tag

I'm implementing a custom permissions application in my Django project, and I'm lost as to how to implement a custom template tag that checks a logged in user's permissions for a specific object instance and shows a piece of HTML based on the outcome of the check.

What I have now is (pseudocode):

{% check_permission request.user "can_edit" on article %}
    <form>...</form>
{% endcheck %}

('check_permission' is my custom template tag).

The templatetag takes in the user, the permission and the object instance and returns the enclosed HTML (the form). This currently works fine.

What I would like to do however, is something like:

{% if check_permission request.user "can_edit" on article %}
    <form>...</form>
{% else %}
    {{ article }}
{% endif %}

I've read about the assignment tag, but my fear is that I would pollute the context variable space with this (meaning I might overwrite previous permission context variables). In other words, as the context variables are being defined on different levels (the view, middleware in my case, and now this assignment template tag), I'm worried about maintainability.

Upvotes: 14

Views: 10664

Answers (5)

mathandy
mathandy

Reputation: 2010

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 %}

Upvotes: 2

andreibosco
andreibosco

Reputation: 158

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

Upvotes: 2

sheikhsalman08
sheikhsalman08

Reputation: 424

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. :)

Upvotes: 0

clarete
clarete

Reputation: 465

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.

Upvotes: 18

Daniel Roseman
Daniel Roseman

Reputation: 599480

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.

Upvotes: 20

Related Questions