Re: Template tag and combining variables & text

2007-07-30 Thread Tomas Kopecek

Emanuele Pucciarelli napsal(a):
> 
> 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 '' % 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,
> 
It looks like a good idea, thanks.

-- 

Tomas Kopecek
e-mail: permonik at mesias.brnonet.cz
 ICQ: 114483784

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



Re: Template tag and combining variables & text

2007-07-29 Thread Emanuele Pucciarelli


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 '' % 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
-~--~~~~--~~--~--~---



Template tag and combining variables & text

2007-07-29 Thread Tomas Kopecek

Hello,
imagine tag, that has one parameter. Typical usage will look like this:

{% img img_url %} or {% img "http://x.y/z.gif; %}

These two cases are simple to implement, the problem arises when 
parameter would be a combination of variable content and string - 
something like this:

{% img {{BASE_URL}}/path/z.gif %}

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

-- 

Tomas Kopecek
e-mail: permonik at mesias.brnonet.cz
 ICQ: 114483784

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