views:

142

answers:

2

I'm new to Django. Today I created some Django custom tags which is not that hard. But now I wonder what is the best way to include some jQuery or some Javascript code packed into my custom tag definition. What is the regular way to include a custom library into my code? For example:

{% faceboxify item %}

So assume that it'll create a specific HTML output for Facebox plugin. I just want to learn some elegant way to import this plugin into my code. I want the above definition to be enough for all functionality. Is there any way to do it? I couldn't find any example. Maybe I'm missing something..

Thank you.

A: 

You can have your custom tag apply "class" values for your Javascript to look for. Include your Javascript as a regular .js <script> import, and have it use a load-time function to find your elements and manipulate them as necessary.

So for example if your tag creates a <div>, you can have it do this:

<div class='faceboxify'>
  <!-- whatever -->
</div>

Your Javascript can then do this:

$(function() {
  $('div.faceboxify').each(function() {
    // ... stuff to be done to your "faceboxify" divs
  });
});
Pointy
Thanks. My question was the second part of your code, also the <script> import statement. Should I output all together <script type='text/javascript' src='facebox.js'></script><script type='text/javascript'>$(function() { $('div.faceboxify').each(function() { // ... stuff to be done to your "faceboxify" divs });});</script> And then:<div class='faceboxify'> <!-- whatever --></div>
pocoa
I would like to find a way to insert the second part of the code to the common $(document).ready(function(){// here} function of the page and insert the import statement to the top.
pocoa
Well I'm not a Django expert so I can't really comment on the "best" way to do it. In the template systems I'm familiar with, I would probably *not* have the tag code write out the `<script>` tag. I'd make sure the script was included on appropriate pages through the use of cooperating templates for page headers. Whether that's a good or bad thing in Django, I cannot say.
Pointy
Okay, thank you anyway!
pocoa
+1  A: 

From the documentation you can see that writing template tags involves writing a target function and a renderer. So I'm assuming your current code looks like this:

def my_tag(parser, token):
  # ... some code
  return MyNode(...)

class MyNode(template.Node):
  def render(self, context):
     # here is where you write your <script> tags

So essentially what you have to do is to hang a variable under the context so you can know if for that specific request you've already included the code to load all your needed scripts.

class MyNode(template.Node):
  def render(self, context):
     if '_included_faceboxify_deps' in context:
       # render your <script> dependency includes
       context['_included_faceboxify_deps'] = True
     # render your <script>s that are specific for this call

This should do the trick. It's not as elegant as including your dependencies in the top of the page, but it's enough not to include them for every time you need to call them.

Miguel Ventura
Thank you. I guess this is what I was looking for.. I'll try soon..
pocoa