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:

  1. create file called base_extras.py in 1 of templatetags folders. if don't have one, create 1 in appropriate folder.
  2. 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 "" 
  3. 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> 
  4. this make menu url has active class. then, in css, add special class menu item active 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

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -