javascript - Append Django template tag with Jquery -
i want build menu can set 1 link highlighted using {% block %} tag. have in javascript:
<loop> $('#a-div').append('{% block ' + variable + ' %} <a href...</a> {% endblock %}') <endloop>
in source, displayed "{% block home %}"
how can make jquery not append string template tag?
you can't. @ least not without making ajax request django template. in case, slow , make unnecessary requests. it's not worth it. can insert snippets django templates via jquery using, example, jquery load
function. can't replace specific {% block %}
tag, because time jquery runs, template has been processed (and references block tags removed). not situation should doing in case.
why don't rather highlight menu css class? here usual solution problem:
- create file called
base_extras.py
in 1 oftemplatetags
folders. if don't have one, create 1 in appropriate folder. inside
base_extras.py
, paste code:from django import template django.core.urlresolvers import reverse register = template.library() @register.simple_tag def navactive(request, urls): if request.path in ( reverse(url) url in urls.split() ): return "active" return ""
now, in template, on menus in base template, this:
<ul class="menu"> <li class="home {% navactive request 'home' %}"><a href="{% url home %}">home</a></li> <li class="contact {% navactive request 'contact' %}"><a href="{% url contact %}">contact</a></li> <li class="signup {% navactive request 'signup' %}"><a href="{% url signup %}">sign up</a></li> </ul>
this make menu url has
active
class. then, in css, add special class menu itemactive
different other menus.ul.menu li.active {background: red; color: white;}
and if happen need change active menu jquery, can remove active
class on menus, , add newly selected menus:
$('ul.menu li').removeclass('active').find('.home').addclass('active'); // example
Comments
Post a Comment