A simple tutorial on using the Mako Template Library in Python _python

Source: Internet
Author: User
Tags locale postgresql redis


Mako is a high-performance Python template library, and its syntax and APIs draw on a number of other template libraries, such as Django, JINJA2, and so on.

Basic Usage

The most basic way to create a template and render it is to use the Template class:


From mako.template import Template
t = template (' Hello world! ')
Print T.render ()


Text parameters passed to Template are compiled into a python module. The module contains a render_body () function that produces the output of the template. When the render () method is invoked, Mako establishes a template's running environment and invokes the Render_body () function, saving the output to a buffer and returning its string contents.
A set of variables can be accessed in the Render_body () function. You can send additional keyword parameters to the render () method to specify these variables:


From mako.template import Template
t = template (' Hello, ${name}! ')
Print T.render (name= ' yeolar ')


The render () method enables Mako to create a context object that stores all the variables that the template can access and a buffer to hold the output. You can also create the context yourself by using the Render_context () method to render the template for rendering:


From mako.template import Template to
mako.runtime import context from
Stringio import stringio
t = Templat E (' Hello, ${name}! ')
BUF = Stringio ()
c = Context (buf, name= ' yeolar ')
T.render_context (c)
print Buf.getvalue ()



Working with file templates

Template can also load a template from a file, using the filename parameter:


From mako.template import Template
t = template (filename= '/docs/tpl.txt ')
print T.render ()


To improve performance, Template loaded from files can also cache the generated modules in the file system as generic Python module files (. py files), which are implemented by adding Module_directory parameters:


From mako.template import Template
t = template (filename= '/docs/tpl.txt ', module_directory= '/tmp/mako_modules ')
Print T.render ()

using Templatelookup

After the above code is rendered, a/tmp/mako_modules/docs/tpl.txt.py file is created that contains the source code for the module. This module file is automatically reused the next time the Template of the same parameter is created.


The current example is all about the use of a single Template object. If the code in your template wants to locate other template resources, you need some way of using URIs to find them. This requirement is achieved by the Templatelookup class. This class is constructed by passing in a template to find a list of directories, and then passed to the Template object as a keyword parameter:


From mako.template import template from
mako.lookup import templatelookup
lookup = templatelookup (directories= ['/docs '])
t = Template (' <%include file= ' header.txt "/> Hello word! ', lookup=lookup)


The template created above contains file header.txt. In order to find Header.txt, a Templatelookup object was passed to it.
Typically, the application stores most or all of the templates on the file system in the form of a text file. A real application will get its template directly from Templatelookup, using the Get_template () method, which accepts the URI of the required template as an argument:


From mako.template import template from
mako.lookup import templatelookup
lookup = templatelookup (directories= ['/docs '], module_directory= '/tmp/mako_modules ')
def serve_template (T_name, **kwargs):
  t = lookup.get_ Template (t_name)
  print T.render (**kwargs)


In the example above we created a templatelookup that looks for templates from the/docs directory and stores all the module files in the/tmp/mako_modules directory. Locating the template by attaching the incoming URI to each lookup directory, such as passing/etc/beans/info.txt, finds the file/docs/etc/beans/info.txt and throws the Toplevelnotfound exception if it is not found.
When you navigate to the template, the URI passed to the Get_template () call is also used as the template URI attribute. Template uses this URI to get the name of the module file, so the/etc/beans/info.txt will create the module file/tmp/mako_modules/etc/beans/info.txt.py in the above example.

set the size of the collection

Templatelookup also satisfies the total number of cached templates in memory set to a fixed value. The default templatelookup size is unlimited. You can specify a fixed value with the Collection_size parameter:


Lookup = Templatelookup (directories=['/docs '), module_directory= '/tmp/mako_modules ', collection_size=500)


The upper limit of lookup to load a template into memory is 500. It will then use the LRU policy to clean up the replacement template.


set up file system checks

Another important sign of Templatelookup is filesystem_checks. The default is True, and each get_template () method returns a template that compares the modification time of the original template file with the most recent load time of the template, and reloads and compiles the template if the file is updated. In a production system, setting Filesystem_checks to False can yield some performance gains.

using Unicode and encoding

Template and Templatelookup can set the output_encoding and Encoding_errors parameters to encode the output as a Python-supported encoding format:


From mako.template import template from
mako.lookup import templatelookup
lookup = templatelookup (directories= ['/docs '], output_encoding= ' utf-8 ', encoding_errors= ' replace ')
t = lookup.get_template (' foo.txt ')
print T.render ()


With Python 3 o'clock, if output_encoding is set, the render () method returns a Bytes object, or string.
The Render_unicode () method returns the template output as a Python Unicode object, and Python 3 is a string:


Print T.render_unicode ()


The above method has no output encoded parameters and can be encoded by itself:


Print T.render_unicode (). Encode (' utf-8 ', ' replace ')


Note the underlying output stream for the template in Mako is a Python Unicode object.


Handling Exceptions

Template exceptions can occur in two places. One is when you find, parse, and compile templates, one is when you run the template. Exceptions that occur in a template run are normally thrown at the Python code where the problem occurs. Mako has its own set of exception classes, which are used primarily for the lookup and compilation phases of template constructs. Mako provides some library routines to provide mako information to the exception stack and output the exception to text or HTML format. Python filename, line number, and code fragment are converted to Mako template filename, line number, and code fragment. The rows of the Mako template module are converted to the original template file corresponding row.



The Text_error_template () and html_error_template () functions are used to format exception tracking. They use Sys.exc_info () to get the most recently thrown exception. The use of these processors is as follows:


From Mako import Exceptions
try:
  t = lookup.get_template (URI)
  print t.render ()
except:
  print Exceptions.text_error_template (). Render ()
or render as HTML: from
Mako import Exceptions
try:
  t = Lookup.get_template (URI)
  print t.render ()
except:
  print Exceptions.html_error_template (). Render ()


The Html_error_template () template accepts two options: Specifies that the Full=false only renders the HTML section, specifying that Css=false closes the default style sheet. Such as:


Print Exceptions.html_error_template (). Render (Full=false)


HTML render functions can also be added to the Template using the Format_exceptions flag. In this case, the result of any exception in the render phase of the template in the output will be replaced with the output of Html_error_template ():


t = Template (filename= '/foo/bar ', format_exceptions=true)
print T.render ()


Note the compile phase of the template above occurs when the Template is constructed, and no output stream is defined. So the exception of the lookup, parse, and compile phases is normally not handled, but spread. Pre-rendering tracing does not include rows in mako form, which means that exceptions that occur before and during rendering are handled in different ways, so try/except may be more commonly used.



The underlying object used by the error template function is the Richtraceback object. This object can also be used directly to provide a custom error view. The following is a sample usage:





from mako.exceptions import RichTraceback
try:
  t = lookup.get_template(uri)
  print t.render()
except:
  traceback = RichTraceback()
  for (filename, lineno, function, line) in traceback.traceback:
    print 'File %s, line %s, in %s' % (filename, lineno, function)
    print line, '\n'
  print '%s: %s' % (str(traceback.error.__class__.__name__), traceback.error)


Integrated Mako
Integrating Mako in Django


The Mako can be integrated through Django middleware. First you need to install the Django-mako module.
In the Django project's settings.py file, modify middleware_classes and add Djangomako.middleware.MakoMiddleware. Use the Render_to_response () function to:


From djangomako.shortcuts import render_to_response
def hello_view (Request): Return
  render_to_response (' Hello.txt ', {' name ': ' Yeolar '})


Integrating Mako in tornado


You can use Mako directly in tornado, and here is an example of using:





import tornado.web
import mako.lookup
import mako.template
LOOK_UP = mako.lookup.TemplateLookup(
    directories=[TEMPLATE_PATH], module_directory='/tmp/mako',
    output_encoding='utf-8', encoding_errors='replace')
class BaseHandler(tornado.web.RequestHandler):
  def initialize(self, lookup=LOOK_UP):
    '''Set template lookup object, Defalut is LOOK_UP'''
    self._lookup = lookup
  def render_string(self, filename, **kwargs):
    '''Override render_string to use mako template.
    Like tornado render_string method, this method
    also pass request handler environment to template engine.
    '''
    try:
      template = self._lookup.get_template(filename)
      env_kwargs = dict(
        handler = self,
        request = self.request,
        current_user = self.current_user,
        locale = self.locale,
        _ = self.locale.translate,
        static_url = self.static_url,
        xsrf_form_html = self.xsrf_form_html,
        reverse_url = self.application.reverse_url,
        )
      env_kwargs.update(kwargs)
      return template.render(**env_kwargs)
    except:
      # exception handler
      pass
  def render(self, filename, **kwargs):
    self.finish(self.render_string(filename, **kwargs))

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.