I want to write a custom function and pass it unto my tornado template fine.
Like def trimString(data): return data[0:20]
then push this into my tornado file.
This should allow me trim strings.
Is this possible?
Thanks.
I want to write a custom function and pass it unto my tornado template fine.
Like def trimString(data): return data[0:20]
then push this into my tornado file.
This should allow me trim strings.
Is this possible?
Thanks.
You can import functions in Tornado. I think this is the cleanest solution. At the top of your template simply do the following:
later you can do
You can also pass the function in as a template variable like this:
Then in your template you call it like this:
It's not especially clear in the documentation, but you can do this easily by defining this function in a module and passing the module to
tornado.web.Application
as theui_methods
argument.I. E.:
in ui_methods.py:
in app.py:
in main.html:
Andy Boot's solution also works, but it's often nice to have functions like this automatically accessible in every template.