List Info

Thread: How to pass a variable as parameter to a tag




How to pass a variable as parameter to a tag
user name
2006-02-15 07:42:31
Say I have a tag, it could take some parameter, just like:

{% calendar 2006 2 %}

But in above code, the parameter is a fixed value. But if I
have a
year and a month variable, and I want to pass them to
calender tag, so
how to deal with that. It seems that:

{% calendar year month %}

can not work.

--
I like python!
My Blog: http://www.donews.net/l
imodou
NewEdit Maillist: http://groups.
google.com/group/NewEdit
How to pass a variable as parameter to a tag
user name
2006-02-15 07:49:14
doesn't something like:
class TopTagsNodeUser(template.Node):
    """ what is the root of this
application """
    def render(self, context):
        user = context.get('user', None)
        if user.is_anonymous():
            return ""
        return "<div
class='panel'><h4>User
Prefs</h4><ul><li>"+str(user)+"
;</li>
</ul></div>"

work?

On 2/15/06, limodou <limodougmail.com> wrote:
>
> Say I have a tag, it could take some parameter, just
like:
>
> {% calendar 2006 2 %}
>
> But in above code, the parameter is a fixed value. But
if I have a
> year and a month variable, and I want to pass them to
calender tag, so
> how to deal with that. It seems that:
>
> {% calendar year month %}
>
> can not work.
>
> --
> I like python!
> My Blog: http://www.donews.net/l
imodou
> NewEdit Maillist: http://groups.
google.com/group/NewEdit
>


--
IanHolsman.net -- blog: http://feh.holsman.net/
-- PH: ++61-3-9877-0909

If everything seems under control, you're not going fast
enough. -
Mario Andretti
How to pass a variable as parameter to a tag
user name
2006-02-17 09:56:36
limodou wrote:

>{% calendar year month %}
>  
>
It's perfectly possible (strange I can't find it in the
docs now but I 
knew it from there).

In your tag function you should wrap all the parameters in a
special 
parser object which then will be evaluated with a context:

def do_calendar(parser, token):
  bits = token.contents.split()
  return CalendarNode(parser.compile_filter(bits[1]), 
parser.compile_filter(bits[2]))

This parser.compile_filter creates such object from the
parameters. It 
can accept variables with filters as well as constant
strings. You can't 
actually evaluate these bits right now because parsing
happens before 
real values passed into template. It's your class' render
method where 
you can resolve these parsed bits with given context:

class CalendarNode(template.Node):
  def __init__(self, year_expr, month_expr):
    self.yesr_expr, self.month_expr = year_expr, month_expr

  def render(self, context):
    year = self.year_expr.resolve(context)
    month = self.month_expr.resolve(context)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

How to pass a variable as parameter to a tag
user name
2006-02-17 10:10:43
On 2/17/06, Maniac <Maniacsoftwaremaniacs.org>
wrote:
>
> limodou wrote:
>
> >{% calendar year month %}
> >
> >
> It's perfectly possible (strange I can't find it in
the docs now but I
> knew it from there).
>
> In your tag function you should wrap all the parameters
in a special
> parser object which then will be evaluated with a
context:
>
> def do_calendar(parser, token):
>   bits = token.contents.split()
>   return CalendarNode(parser.compile_filter(bits[1]),
> parser.compile_filter(bits[2]))
>
> This parser.compile_filter creates such object from the
parameters. It
> can accept variables with filters as well as constant
strings. You can't
> actually evaluate these bits right now because parsing
happens before
> real values passed into template. It's your class'
render method where
> you can resolve these parsed bits with given context:
>
> class CalendarNode(template.Node):
>   def __init__(self, year_expr, month_expr):
>     self.yesr_expr, self.month_expr = year_expr,
month_expr
>
>   def render(self, context):
>     year = self.year_expr.resolve(context)
>     month = self.month_expr.resolve(context)
>

It sounds very excellent. I'll try it. Thank you very much.

--
I like python!
My Blog: http://www.donews.net/l
imodou
NewEdit Maillist: http://groups.
google.com/group/NewEdit

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )