Quote:
Originally Posted by capink
When writing a template function, is there a way to test whether the function is called from a python template or not?
|
Not easily. You can examine the stack and see if "call" is method name in the calling frame, but I think this might be brittle. See below for another idea.
Quote:
If so, can it return a python object (e.g. python list)? or would it result in an error?
|
Yes, a python template function can return a python object. The formatter returns it to the calling template unchanged.
I suggest you provide two functions, one that returns a string and one that returns whatever. Alternately, provide an optional parameter that tells the function what to return.
Note that if a python template function returns a list then the formatter tries to convert the returned value to a string with ', '.join(returned_value). Of course this only works if the list contains strings.