简体   繁体   中英

def function in python in a tpl file?

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM