Mako Expression Filter (5)

Source: Internet
Author: User

By default

U-URL

H-html

X-XML

Trim-string. Strip ()

Entity-HTML Entity

Unicode-default Unicode string

Decode

N-cancel all default Filters

 

Multiple filters are separated by comma (,). You can also define a filter by yourself.

 

 
<%!DefMyescape (text ):Return "<Tag>"+ TEXT +"</Tag>"%>

Here's some tagged text :$ {"text" | myescape}

The custom filter must be a parameter that supports a string and return the filtered result.

 

Use expression_filter in the <% PAGE> label to use this filter for the entire page.

 

You can also set filter for block and def.

 

 
<%DefName ="Foo ()"Filter ="H, trim"> <B> ThisIsBold </B> </%Def>

 

 

In execution

$ {"Results"+ Somedef () +"More results"}

Will return (assuming somedef returns"Somedef's Results"String)

 
Somedef'S results more results

This is because somedef () is fully executed before the string link, the link string will get an empty string return value. To avoid this problem, Mako has two methods:

 

<%DefName ="Somedef ()"Buffered ="True">Somedef'S results</%Def>

The aboveCodeThe following code is generated:

DefSomedef (): context. push_buffer ()Try: Context. Write ("Somedef's results")Finally: Buf=Context. pop_buffer ()ReturnBuf. getvalue ()

Or use

 
$ {"Results"+ Capture (somedef) +"More results"}

Capture is similar to the python apply method. The parameter passed to somedef can be passed to somedef by capture.

 

In order to make def more flexible to filter, decorator also supports filter in template.

 

<% !  Def  Bar (FN ):  Def Decorate (context, * ARGs ,** KW): context. Write (  "  Bar  "  ) FN ( * ARGs ,** KW) Context. Write (  " Bar  "  )  Return   ''          Return  Decorate %> <% Def Name = "  Foo ()  " Decorator = "  Bar  " > This  Is Foo </% Def > $ {Foo ()} 

 

 

 

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.