Mako Template Library Documentation
repository·main·Indexed 19 days ago
https://github.com/sqlalchemy/makoA high-performance Python template library that compiles templates into Python modules. It features an embedded Python syntax, layout inheritance, and componentization. Documentation covers basic template syntax, the use of tags for control flow and expression evaluation, and the Mako command-line interface for rendering templates.
What's inside Mako
- Mako is a Python template library that uses a non-XML syntax and compiles templates into Python modules for high performance. It is designed as an embedded Python language (similar to Python Server Pages), allowing for close integration with Python's calling and scoping semantics. It supports advanced features like componentized layouts, template inheritance, and defining reusable functions within templates.
Basic Mako Template Syntax and Usage
mainMako templates allow you to mix HTML with Python code using specific tags:
<%inherit file="..."/>: Used for template inheritance.<% ... %>: Used for blocks of Python code (e.g., variable assignments).% for ... :and% endfor: Used for Python control flow like loops.${...}: Used for expression evaluation (outputting values to the template).<%def name="..."> ... </%def>: Used to define reusable template functions (components).
<%inherit file="base.html"/> <% rows = [[v for v in range(0,10)] for row in range(0,10)] %> <table> % for row in rows: ${makerow(row)} % endfor </table> <%def name="makerow(row)"> <tr> % for name in row: <td>${name}</td> % endfor </tr> </%def>Mako CLI command options and flags
mainThe Mako CLI supports the following arguments and options for controlling template rendering:
Option Argument Description --varname=valueDefine template variables. This flag can be used multiple times. --template-dirdirectoryDirectories to use for template lookup. Can be used multiple times. If not provided and reading from stdin, defaults to the current directory. If reading from a file, defaults to the file's parent directory.--output-encodingencodingForce a specific output encoding for the rendered content. --output-filepathWrite the rendered result to this file instead of printing to stdout.input(positional) The template file to render. If omitted or set to -, Mako reads fromstdin.mako [input] [--var name=value] [--template-dir dir] [--output-encoding encoding] [--output-file file]Use the Mako CLI to render templates
mainThe Mako command-line interface allows you to render Mako templates directly from your terminal. You can provide a template file as a positional argument or pipe content via
stdin. The rendered output is sent tostdoutby default, but can be redirected to a file using the--output-fileflag. You can also pass variables to the template using the--varflag.# Render a template file and save to an output file with variables mako my_template.txt --var name=World --var age=30 --output-file result.txt # Render from stdin cat my_template.txt | mako --var name=StdinUser