Re: Templates: dynamic attribute referencing?

2007-08-08 Thread Collin Grady

Your filter would work fine, you just need to stop trying to add
quotes and {{}} to it ;)

{{ obj1|get_attr_by_name:obj2.foo }}


--~--~-~--~~~---~--~~
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: Templates: dynamic attribute referencing?

2007-08-08 Thread Doug B

See if this tag won't work for you:

class ObjectAttribNode(template.Node):
def __init__(self, obj, attr):
self.obj = obj
self.attr = attr

def render(self, context):
print self.attr
try:
obj = resolve_variable(self.obj, context)
attr = resolve_variable(self.attr, context)
return str(getattr(obj,attr,''))
except template.VariableDoesNotExist:
return ''

def do_val_by_object_attrib(parser, token):
try:
# split_contents() knows not to split quoted strings.
tag_name, obj, attr = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError, "%r tag requires exactly
two arguments" % token.contents.split()[0]
return ObjectAttribNode(obj, attr)

register.tag('val_by_object_attrib', do_val_by_object_attrib)


Then in template:
{% val_by_object_attrib object attribute_name %}

where object, and attribute_name are both variables in the 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-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
-~--~~~~--~~--~--~---



Templates: dynamic attribute referencing?

2007-08-08 Thread rskm1

(I know I can't simply _NEST_ {{}}'s in a template, but I'm using it
as pseudosyntax to help express what I'm _trying_ to do.)


Is there any way, in a template, to get an attribute of an object when
the NAME of that attribute is dynamic (i.e. in/from another object)?

What I really want is:
  {{ obj1.{{ obj2.attname }} }}  {# NOPE #}
or somewhat Pythonese:
  {{ obj1.__getattr__( {{ obj2.attname }} ) }}  {# NOPE #}

I thought I could be tricky and write a filter called "getattrbyname"
that would return only the specified attribute of the object...
...but that won't work either, because I'm not sure how to get the
name parameter to it from the template.
  {{ obj1|getattrbyname:"{{ obj2.attname }}" }}  {# NOPE #}


Is there another approach that would actually WORK?

(Note: This is kind of an "overzealous novice" question, so don't rule
out the possibility that I totally overlooked some obvious solution.
=)



If you're wondering how I came up with such a goofy request:
My application is displaying a table with information about a "key" in
each row.
I thought it would be cool to extend the table-header-sorting
capability (djangosnippet #308), to make headers() return the
order_criterion as 'columnname' in each entry.
Then I could reuse the same template for lots of different subsets of
the same table, simply by changing the set of headers I pass in.
(Because sometimes I want the table to contain more or less detail
about each "key")

Like so:

  

{% for header in headers %}
{% if header.sortable %}
  {{ header.text|escape }}
{% else %} {# !header.sortable #}
  {{ header.text|escape }}
{% endif %} {# header.sortable #}
{% endfor %} {# headers #}


{% for key in keylist %}
  
{% for header in headers %}

  {# NOPE, THIS DOESN'T WORK! #}
  {{ key.{{ header.columnname }}|escape }} {# NOPE #}

{% endfor %} {# headers #}
  
{% endfor %} {# keylist #}

  


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