Question

Can you define a function inside of a template file? If not, can you import a custom python lib in the .tpl file to call a function?

This is the function I am trying to call:

%def rec_print(obj):
    %if "output" not in rec_print.__dict__: rec_print.output = ""
    %end
    %if isinstance(obj, unicode):
        %rec_print.output += "<li>"+str(obj)+ "</li>"
    %elif isinstance(obj, dict):
        %for k, v in obj.items():
            %rec_print.output += "<li>" +str(k) +"</li><ul>"
            %rec_print(v)
            %rec_print.output += "</ul>\n"
    %elif isinstance(obj, list):
        %for items in obj:
            %rec_print(items)
    %else:
        %print "uknown type for", obj, type(obj)
    %end
    %return rec_print.output
%end

I use this function to generate content that I get from my mongodb in a bottle powered webapp.

Was it helpful?

Solution

Templates: Fast and pythonic built-in template engine and support for mako, jinja2 and cheetah templates.

From bottle's website.

You can use Mako, which is a templating language for Python. You can use it with bottle. Here is a section on Defs and Blocks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top