Today and everyone to share their own in the process of learning the Bottle framework of the template of the knowledge points, I hope to help Everyone.
Bottle uses its own compact Template.
You can use the call template (template_name, **template_arguments) and return the Result.
@route ('/hello/:name ') def Hello (name):
Return template (' hello_template ', Username=name)
This loads the HELLO_TEMPLATE.TPL andextracts the url:name to the variable username, returning the Request.
Hello_template.tpl roughly like this :
Template Search Path
The template is based on Bottle. Template_path list variables to Search.
the default path contains ['./%S.TPL ', './views/%s.tpl '] .
Template caching
The template is cached in memory after Compilation.
Modifying the template does not update the cache until you clear the Cache.
Call Bottle. Templates.clear ().
Template syntax
template syntax is around Python is a thin layer.
The main purpose is to ensure that the correct indentation is in the Block.
Here are some examples of template syntax :
%... The Python code begins. You do not have to deal with indentation Issues. Bottle will do this for YOU.
%end Close some statements %if ... , %for ... or Something. Closing blocks is a Must.
{{...}} Print out Python The result of the Statement.
%include template_name optional_arguments includes other Templates.
Each row is returned as Text.
Example:
%header = ' Test Template '
%items = [all-in-all, ' Fly ']
%include http_header title=header, use_js=[' jquery.js ', ' Default.js ']
<ul>
%FOR Item in Items:
<li>
%if isinstance (item, int):
Zahl: {{item}}
%else:
%try:
Other type: ({{type (item). __name__}}) {{repr (item)}}
%except:
Error:item has no string representation.
%end Try-block (yes, You could add comments Here)
%end
</li>
%end
</ul>
%include Http_footer
article from:segmentfault
Detailed bottle Template