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.