1

I have setup a custom template tag (simple_tag) (using https://stackoverflow.com/a/7716141/1369798) with a definition like this:

templatetags/polls_extras.py

def settings_value(name)

which I am able to use in my template like this:

templates/index.html

{% settings_value "ALLOWED_BOOL" %}

But this just inserts the text into my HTML output.

What is the syntax to use my template tag with parameter in an {% if %}?

I tried this but I get the error: TemplateSyntaxError at / Unused '"ALLOWED_BOOL"' at end of if expression.

templates/index.html

{% if settings_value ALLOWED_BOOL %}
You are allowed.
{% endif %}
Community
  • 1
  • 1
Alveoli
  • 1,202
  • 17
  • 29
  • Similar to http://stackoverflow.com/questions/11372177/django-template-tag-in-if-block but I want to add a parameter in! – Alveoli Sep 03 '14 at 13:16
  • Are you sure you can't sent this value to template from view? It seems not usual to set a new variable in template to use in it. – dani herrera Sep 03 '14 at 13:28
  • Nope... it's needed in every page. Have taken into accoubt discussion in http://stackoverflow.com/a/7716141/1369798 and this is the best solution... just want to know how to do it! – Alveoli Sep 03 '14 at 13:31
  • 1
    I think that better approach for you is a custom context processor. A custom context processor can 'inject' automatically new data to all templates. – dani herrera Sep 03 '14 at 13:41

1 Answers1

1

You cannot use a templatetag as a parameter to another templatetag. Your options here are either

  1. modifying your settings_value templatetag so it can inject the value in the current context, ie :

    {% settings_value ALLOWED_BOOL as allowed_bool %} {% if allowed_bool %} You are allowed. {% endif %}

Note that simple_tag won't work here, you'll either have to switch to assignement_tag (if your Django version support it) - but then you'll loose the ability to directly output a setting in the template the way you actually do - or write a full blown custom templatetag (which is not as difficult as it might seem at first).

  1. Use a custom context_processor instead like danhip suggests - but then only templates rendered using a RequestContext will access these variables.
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118