tags:

views:

70

answers:

1

Hi, I just started learning about custom tags in django and was wondering if there was a feature that allowed the injection of data as if they were blocks/tags.

The real problem is this, tags work great, but lets say my tag is some html and javascript, does that mean I have to call two tag functions and inject them into the page that way. It almost seems like the solution is to use a template where you fill in the blocks and it appends the data that way, but that can't do what the custom tags need to do. So how would you solve a problem like this?

+1  A: 

You need to use django inclusion tags

Infact, django admin itself uses these tags for very similar purpose.

From the documentation, Define a function like this, which is aware of the template it needs to render from

@register.inclusion_tag('results.html')
def show_results(poll):
    choices = poll.choice_set.all()
    return {'choices': choices}

And the template:

<ul>
{% for choice in choices %}
    <li> {{ choice }} </li>
{% endfor %}
</ul>

Then you insert a tag as follows:

{% show_results poll %}

Which will provide:

<ul>
   <li>First choice</li>
   <li>Second choice</li>
   <li>Third choice</li>
</ul>
Lakshman Prasad
hi, thanks for responding :)you know what, initially I wanted to lump all the javascript together into the head and that's why I mentioned that I wanted to load the script in two different spots on the page. Thinking it over, I guess it really doesn't matter where the javascript is at, right? Would there be a slow down if every item had its own script and onload function? If not, then loading the javascript with the html may not be such a bad thing afterall.
You can either load the js in the header and forget about it in the html loaded; or you can load the js with the html in each block. You have to make the trade off depending on how many pages and how many times in each page this appears.
Lakshman Prasad