Monday, April 30, 2012

Re: Parse Custom html in custom template tags, django 1.4

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.

On Monday, 30 April 2012 09:24:54 UTC-3, Daniel Roseman wrote:


On Monday, 30 April 2012 09:03:56 UTC+1, 95felipe wrote:

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!


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.

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

No comments:

Post a Comment