On 22 jan, 01:59, Andrew Ingram <a...@andrewingram.net> wrote:
> Hi all,
>
> I'm writing the render method of a template tag, and I want to make the
> format of the tokens quite flexible.
>
> I have a dict of args, some in the format 'string', some just numbers
> like 50 and others references to context variables. I am wondering if
> there's an elegant way of resolving all the values.
>
> ie, my dict is:
>
> args = {
>     'foo': '50',
>     'bar': '\'string\'',
>     'foobar': 'object.blah',
>
> }
>
> If they were all context variables I know I could just use
> Variable('object.blah').resolve(context), but I'm not so sure about how
> to tackle the strings and numbers. I guess some regexps could be used to
> do some basic branching.
>
> Any thoughts would be appreciated.

As often with Python, the simplest thing to do is to fire a Python
shell and explore the problem:

>>> from django import template
>>> c = template.Context(dict(foo='foo', bar=range(3), baaz=dict(un=1, deux=2)))
>>> c
[{'baaz': {'un': 1, 'deux': 2}, 'foo': 'foo', 'bar': [0, 1, 2]}]
>>> template.Variable('foo').resolve(c)
'foo'
>>> template.Variable('bar').resolve(c)
[0, 1, 2]
>>> template.Variable('bar.0').resolve(c)
0
>>> template.Variable('baaz.un').resolve(c)
1
>>> template.Variable('50').resolve(c)
50
>>> template.Variable('"string"').resolve(c)
'string'
>>> template.Variable('\'string\'').resolve(c)
'string'
>>>

HTH

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