Rune Bromer wrote:
> On Dec 5, 2007, at 11:59 AM, Michal Ludvig wrote:
> 
>> Hi all,
>>
>> for the project I'm working on I needed to create a custom template  
>> tag,
>> call it 'mytag'.
>>
>> If I use it as {% mytag something %} everything goes fine (where
>> 'something' is a string passed to the template).
>> However I can't add any filters to that 'something'. As soon as I do:
>> {% mytag something|upper %}
>> I get an exception:
>>      VariableDoesNotExist at /test/
>>      Failed lookup for key [something|upper]
>>
>> That leads to a code in MytagNode.render() method:
>>      actual_message = resolve_variable(self.message, context)
>> where self.message is the argument passed to the macro, in this case
>> it's a string 'something|upper'.
>>
>> Is there an easy way to run all the attached filters in MytagNode?
> 
> Look at the "filter" templatetag [1]. You must do something like
> 
> {% filter upper %} {% mytag something %} {% endfilter %}

That seems to run the whole output of mytag through the filter. I just
need the parameter itself filtered. In reality mytag gets two arguments,
'label' and 'value' and spits out a few lines of HTML. It displays some
user profile information. The value of each property may need to be
filtered differently, e.g. if it's a dollar amount I want to attach my
filter '|dollarformat' that turns an integer like 1000 to a more
readable "$1,000". For other properties I may want other filters. In any
case I only need to filter the argument, not the tag output.

I could probably do something like message.split("|") and run the
filters (if any) manually. That is, import them from
django.template.defaultfilters and pass the output of resolve_variable
through them. But what if the filter in question is imported from a
non-default tag library? How do I find what to import then?

BTW Wouldn't it be more user-friendly to resolve the variable and run
all the filters yet before the custom tag gets called?

Michal




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

Reply via email to