Mako namespace (4)

Source: Internet
Author: User

The namespace of Mako is processed in the <% namespace> format.

 

<% Namespace file = "a.html" Import = "comp1, comp2"/>

 

Used to import the comp1 and comp2 functions in a.html, which are defined by DEF of Mako.

 

You can use the attribute name to name the module again, and then use name to reference it. Function calls are similar to function calls in Python.

 

At the same time, a powerful function is to call the python module.Code, Import the python module through the attribute module, and then use name to reference the module function

 

Note that the function must have at least one context parameter, and any returned value will be render. Therefore, ''is generally returned ''.

 

To use the caller object in the context, supports_caller (in Mako. runtime) is required, as shown in the following two methods:

 

FromMako. RuntimeImportSupports_caller @ supports_callerDefMy_tag (context): context. Write ("<Div>") Context ['Caller']. Body () Context. Write ("</Div>")Return ''

 

FromMako. RuntimeImportSupports_caller, capture @ supports_callerDefMy_tag (context ):Return "<Div> % S </div>"%\ Capture (context, context ['Caller']. Body, x ="Foo", Y ="Bar")

 

Def is also supported in namespace, similar to def in modules.

 

 # Define a namespace  <%  Namespace name  =  "  Stuff  "  >      <  % Def name  = "  Comp1 ()  "  >  Comp1  </  % Def  >  </  % Namespace  >  ## Then   Call  It $ {stuff. comp1 ()} 

 

 

Each namespace has a body function, which generally does not accept parameters. If you need to accept parameters, we recommend that you use the <% PAGE> label to accept parameters.

 

 
<% PAGE ARGs ="X, Y, someval = 8, scope = 'foo', ** kwargs"/>

When the top-level template is called (these parameter values are passed through the render or <% inclue> label ). ** Kwargs obtains the parameter value from ** pageargs. In other cases, you can pass parameters by calling the body method.

 

This feature is useful when inheriting templates!

 

In namespace, there are built-in local and self. Local indicates the current execution template. Self indicates that the inheritance is the same as local when inheritance is not used, and the top-level template is used when inheritance is used.

 

 

When the inheritable attribute in namespace is true, it indicates that the namespace part can be loaded into the namespace of self. Then, the template information can be accessed in all inheritance chains.

 

 

 

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.