0

I am trying to solve a problem. During the process I got this question.

{% if request.path == '/{{sub_cat.sub_cat_parent.cat_slug}}/{{sub_cat.sub_cat_slug}}/' %} 
            <div class="alert-secondary">Active</div>
{% else %}

How do I properly include slashes in here '/{{sub_cat.sub_cat_parent.cat_slug}}/{{sub_cat.sub_cat_slug}}/'? Is it okay to use {{ within {% template tags? what am I doing wrong in here?

Mir Stephen
  • 1,758
  • 4
  • 23
  • 54
  • try ```'\/{{sub_cat.sub_cat_parent.cat_slug}}\/{{sub_cat.sub_cat_slug}}\/'``` – al76 Jun 17 '19 at 08:10
  • Please include the error message you get (if any). – Maximouse Jun 17 '19 at 08:12
  • @al76 thanks for help, but it didn't work. and MaxiMouse, there is no error shown for this – Mir Stephen Jun 17 '19 at 08:15
  • https://stackoverflow.com/questions/56625356/how-do-i-add-active-class-for-my-current-list-group/56625973?noredirect=1#comment99824508_56625973 this question is related to above question here is the link – Mir Stephen Jun 17 '19 at 08:15

2 Answers2

3

"variable substitution" ({{ somevar }}) syntax is indeed not supported in templatetags arguments - which makes sense if you consider that in the context of template rendering, this syntax would be the equivalent of a "print" statement.

This doesn't mean you can't pass context variables to templatetags - else templatetags would be of very limited use - but you do so by just mentionning them ie assuming you have a context variable named "path", you could use it here as

{% if request.path==path %}
...

You can also use filter expressions here, ie this is valid too:

{% if request.path==path|lower %}

(assuming the templatetag's author correctly handled the tags arguments - I let you refer to the documentation for more on this).

Now wrt/ your problem here, you could of course use a custom filter to join the different parts but that would actually be a very bad idea. Django's url handing is based on the idea that you should actually never hardcode an url anywhere - you define and name urls in your urls.py files, and then use the builtin functions (django.core.urlresolvers.reverse()) / templatetags ({% url %}) to generate them.

Here, the proper solution would be to define a get_absolute_url() method on your sub_cat object that returns the correct url (actually the path portion of it) for this object, and test against it in the template.

As far as I'm concerned, I'd even use a second indirection level as a template filter so 1/ your model (or whatever sub_cat is) knows it's own url, and 2/ the template filter can tell if a given sub_cat is "active" for the current request.path so if the spec defining when a sub_cat is "active" is to change you'll only have the template filter function to change instead of having to browse thru all your templates (possibly missing something on the way).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
0

use with clause

{% with "/"|add:sub_cat.sub_cat_parent.cat_slug|add:"/"|add:sub_cat.sub_cat_slug|add:"/" as url_path %}

    {% if request.path == url_path %} 
      <div class="alert-secondary">Active</div>
    {% else %}

{% endwith %}

this will work