Def and block of mako (2)

Source: Internet
Author: User

In the Mako template, Def and block are all functions that can be called. Def is more similar to Python def and block is more Layout-oriented.

 

Def Basics

Def requires a name attribute, such

<% Def name = "Hello ()">

Hello World

</% Def>

 

Call the following method:

The def :$ {Hello ()}

 

If <% def> is not nested in <% def>, it is called top-level def. All def can be called in the current context.

 

Since def is a python function, all parameters that can be defined and passed must comply with common Python function specifications.

 

 

Def call from other files

The top-level <% def> can be exported to other template modules. It is similar to calling other templates in the <% def> mode.

Import Other templates,

<% Namespace name = "mystuff" file = "mystuff.html">

Then mystuff will be included in the current scope, and then mystuff will be called

$ {Mystuff. somedef (x = 5, y = 7 )}

 

The <% namespace> tab supports Python import statements.

<% Namespace file = "mystuff.html" Import = "Foo, bar"/>

 

 

Call def Programming

You can use get_def to obtain the deftemplate object in the template object.

 

Def nesting

Here, Mako provides two very interesting examples.

 <%      X   =   12  %>  <%  Def   Name =  "Outer ()"  >  <%          Y  =   15      %>  <%  Def   Name =  "Inner ()"  >  Inner, X is  $ {  X  }  , Y is  $ {  Y  }  </%  Def  >  Outer, X is  $ {  X  } , Y is  $ {  Y  }  </%  Def  > 


And

<%X = 10%><%Def Name ="Somedef ()"># Error!Somedef, X is$ {X}<%X = 27%></%Def>


In the second column, X will reference the X of the local variable. At this time, X has not been assigned a value, and an error will be reported!

 

 

 

Call def in Embedded content or other def

You can use <% namespace: defname> to expand the custom tag system. In most cases, local or self represents the namespace of the current template.

 

When the target def is involved, the caller variable is used to replace the output content. The caller contains another namespace defined by body and def. The body is introduced using the method body.

 

 
<%Def Name ="Buildtable ()"><Table><Tr> <TD>$ {Caller.Body()}</TD> </tr></Table></%Def><%SELF: buildtable>I am the table body.</%SELF: buildtable>
 
the caller is used to replace <% self: buildtable note that the names after self must be consistent with those defined by def.
The following example is provided:
  <%   def   name =   "Lister (count) " > %    x   in   range   (  count  ):  $ {  caller  .   body   ()  } %  endfor   
     def     <%   self: lister   COUNT =   "$ {3}"  >  Hi  
      self: lister    
Count ="$ {3 }"So that the provided 3 will be treated as an expression to get the integer 3, rather than the string 3. The parameter of this range must be determined by the integer.


Note that the body function can also pass parameters.


Bocks
<% Block> labels are similar to <% def>. Anonymous blocks are available in partial render. The named block behavior is similar to that of jinja2,
 
Difference from Def:
1. The block declaration does not allow parameter signature.
2. The named block is defined only once in a template. If there is a Def with the same name, an error occurs. If there is a Def with the same name, an error occurs.
3. The named block cannot be called by definitions in DEF or internal bodies.

To enable the named block to share the page parameters, a variable similar to ** pageargs also has a named blocks with The args attribute. This parameter does not exist explicitly in the page tag.







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.