Detailed bottle Template

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.