Il giorno 29/lug/07, alle ore 22:48, Tomas Kopecek ha scritto:

> I know, it's syntactical nonsense. But does anybody know about some  
> way
> how to combine variable content with string content? Is it possible?
>
> For example, very crude way could be something like
>
> {% img %} {{BASE_URL}}/path/z.gif {% endimg %}

Maybe something like {% img base_url "path/z.gif" %}: let string  
content be surrounded by double quotes, and let the tag join  
everything for you.

Code:

from django.template import Node, TemplateSyntaxError, Library

register = Library()

class MixedNode(Node):
        class Variable(object):
                def __init__(self, name):
                        self.name = name
                def __str__(self):
                        return self.name
        def __init__(self, args):
                self.parameters = [
                        [self.Variable(arg), 
arg.rstrip('"').lstrip('"')][arg[0] == arg 
[-1] == '"']
                        for arg in args]
        def get_param(self, i, context):
                if isinstance(i, self.Variable):
                        return context[str(i)]
                else:
                        return i
        def get_string(self, context):
                return ''.join([self.get_param(param, context) for param in  
self.parameters])
                
class ImageNode(MixedNode):
        def render(self, context):
                return '<img src="%s"/>' % self.get_string(context)
        
def do_img(parser, token):
        arglist = token.split_contents()
        if len(arglist) < 2:
                raise TemplateSyntaxError, "%r tag requires at least one 
argument"  
% token.contents.split()[0]
        return ImageNode(arglist[1:])
        
register.tag('img', do_img)

(you can then subclass MixedNode for pretty much everything...)

Regards,

-- 
Emanuele
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to