Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Parse Custom html in custom template tags, django 1.4

43 views
Skip to first unread message

95felipe

unread,
Apr 30, 2012, 4:03:56 AM4/30/12
to django...@googlegroups.com

Hi. I've been struggling with this problem the whole night and just couldn't find any solution. I've tried reverse engineering the template/base.py file, but things are getting ugly. :S

How can I, inside a custom tag class (template.Node), make the parser render a snippet of html with tags in it? For example:

@register.tag(name='addspam')
class AddSpam(template.Node):
   
def __init__(self, parser, token): ...
   
def render(self, context):
        spam_html
= "SPAM { any_tag_here } SPAM"
       
return spam_html

Here, AddSpam, when 'called', returns 'SPAM { any_tag_here } SPAM', without rendering the any_tag_here.That's obviously the predictable, but how can I change the return value so that any_tag_here is rendered as if it was 'native'? Are there any methods using the context and the parser that I could use? Thanks!

Daniel Roseman

unread,
Apr 30, 2012, 8:24:54 AM4/30/12
to django...@googlegroups.com
You could simply instantiate and render a template.Template object with the content of `spam_html` and the existing context:

    spam_tpl = template.Template(spam_html)
    return spam_tpl.render(context)
--
DR.

95felipe

unread,
Apr 30, 2012, 11:06:21 AM4/30/12
to django...@googlegroups.com
Hi! But then the rendering wouldn't include the loaded templatetags.
And I guess it would be expensive to go through this process a couple times for each call of the renderer.
Reply all
Reply to author
Forward
0 new messages