View Single Post
Old 10-29-2023, 05:17 PM   #660
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,467
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
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.
chaley is offline   Reply With Quote