I've just written my first template tag (w/ django-1.3).  The idea is to 
produce the HTML for a UI component which can get embedded in other templates.  
The top-level template is items.html (attached).  My tag is item_html, defined 
in collage_extras.py (also attached).

This works, but what I really want my render() function to do is render another 
template (as opposed to embedding HTML in the python code).  It's not clear how 
to do this.  You can't call render_to_response() from inside a tag.  Or can you?

{% include "collage/header.html" %}

{% load collage_extras %}

{{num_items}} Items:

{% include "collage/footer.html" %}
from django import template

register = template.Library()

def do_item_html(parser, token):
    try:
        tag_name, item = token.split_contents()
    except ValueError:
        tag_name = token.contents.split()[0]
        raise template.TemplateSyntaxError("%r tag requires a single argument" % tag_name)
    return ItemHtmlNode(item)

class ItemHtmlNode(template.Node):
    def __init__(self, item):
        self.item = template.Variable(item)

    def render(self, context):
        my_item = self.item.resolve(context)
        return '<div><div><img src="/static/img-icon.png"></div><div>%s<br>%s</div></div>' % (my_item.id,
                                                                                              my_item.title)
                                                                                              

register.tag('item_html', do_item_html)


--
Roy Smith
r...@panix.com





-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to