On Tue, Nov 22, 2011 at 2:26 PM, vanderkerkoff <tonm...@gmail.com> wrote:
> Thanks Tom
>
> I went for option 2.  Here's what I've done so far, works ok at the
> moment, will need some tidying up and caching.
>
> geturl.py in my mytags/templatetags folder
>
> from django import template
> from django.template import Library, Node
>
> register = Library()
>
> class GetUrlNode(Node):
>        def __init__(self, url, varname):
>                self.url, self.varname = url, varname
>
>        def render(self, context):
>                from urllib2 import urlopen
>                html_as_string = urlopen(self.url).read()
>                context[self.varname] = html_as_string
>                return ''
>
> def get_get_url(parser, token):
>        bits = token.contents.split()
>        if len(bits) != 4:
>                raise TemplateSyntaxError, "get_get_url tag takes exactly three
> arguments"
>        if bits[2] != 'as':
>                raise TemplateSyntaxError, "third argument to get_get_url tag 
> must
> be 'as'"
>        return GetUrlNode(bits[1], bits[3])
>
> get_get_url = register.tag(get_get_url)
>
> mega-menu.html templates/shared that gets called into the base.html
> with an include
>
> {% load geturl %}
> {% get_get_url URL_of_HTML as rendered_html %}
> {% autoescape off %}{{ rendered_html }}{% endautoescape %}
>
> Getting there, slowly :-)
>

Not that there is anything wrong with this, I would have made a simple
tag that simply returns the content - it means you don't have to write
a tedious tag parsing function, or a node class:

from urllib2 import urlopen
from django.utils.safestring import mark_safe
from django.conf import settings

@register.simple_tag
def get_asset_contents(path):
    uri = "%s%s" % (settings.ASSET_SERVER_LOCATION, path)
    return mark_safe(urlopen(uri).read())

{% get_asset_contents "/path/to/asset.html" %}

Cheers

Tom

-- 
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