0

I have a dictionary with a number of characteristics:

sort_options = SortedDict([
    ("importance" , ("Importance" , "warning-sign" , "importance")),
    ("effort" , ("Effort" , "wrench" , "effort")),
    ("time_estimate" , ("Time Estimate" , "time" , "time_estimate")),
])  

I also have a list of actions as a query result. Each action has these attributes; In my template, I can call {{ action.effort }} or {{ action.time_estimate }} and get a result.

I'm iterating through my sort_options to populate twitter bootstrap icons:

{% for key, icon in sort_options.items %}
    <i class="icon-{{ icon.1 }}"></i>
{% endfor %}

But I also want to display the action value for each of these attributed. Essentially, something like:

{% for key, icon in sort_options.items %}
    <i class="icon-{{ icon.1 }}"></i>
    {{ action.key }}
{% endfor %}

Where key would resolve to "importance" or "effort". I know this doesn't work. So I was trying to leverage the solution presented in this question.

The solution proposed a template filter:

def hash(h,key):
    if key in h:
        return h[key]
    else:
        return None
register.filter(hash)

{{ user|hash:item }}

Where the question used a dictionary that looked like so:

{'item1': 3, 'name': 'username', 'item2': 4}

I tried the following:

{% for key, icon in sort_options.items %}
    <i class="icon-{{ icon.1 }}"></i>
    {{ action|hash:key }}
{% endfor %}

But got an error:

Caught TypeError while rendering: argument of type 'Action' is not iterable

I believe this is because the template filter is getting just one attribute of the object (likely the name) as opposed to the whole dictionary:

[<Action: Action_one>, <Action: Task_two>...]

Is there a way to force the template to pass the full object to the template tag?

Community
  • 1
  • 1
Ed.
  • 4,439
  • 10
  • 60
  • 78
  • 1
    Please make sure that you mean a template tag but not a *template filter*. – sergzach Aug 13 '12 at 19:19
  • 1
    Ignore that other question. Exactly what are you trying to achieve? – Daniel Roseman Aug 13 '12 at 19:23
  • Apologies for being too hasty with the question. It is greatly clarified above – Ed. Aug 13 '12 at 19:52
  • 1
    For future reference, mentioning that you're iterating over actions and then iterating over `sort_options` for each `action` would have been handy. Finally, something that looks like `[.., ..]` is a list, not a dictionary. Your "whole dictionary" is better described as a "list of `Action` objects`. – supervacuo Aug 13 '12 at 20:11
  • Can't u just calculate those 'action.key's in your views, and then try sending them as dictionaries to the templates so u won't have to find action.key in your templates ? –  Aug 13 '12 at 20:15
  • I could, but I would rather not create another dictionary to house data that already exists in action. Maybe it's simply a matter of preference, but I don't want multiple variables with the same data. – Ed. Aug 13 '12 at 20:19

1 Answers1

0

I think I finally get it. You want the equivalent of

getattr(action, key)

in your template. This answer describes the getattribute templatetag you'd need to do so.

Community
  • 1
  • 1
supervacuo
  • 9,072
  • 2
  • 44
  • 61