Mako Template Library Documentation

repository·main·Indexed 19 days ago

https://github.com/sqlalchemy/mako

A 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.

Tokens
832
Snippets
3
Records
4
Agent score
16%

What's inside Mako

  1. Overview of Mako Templates

    main
    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.
  2. Basic Mako Template Syntax and Usage

    main

    Mako 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>
  3. Mako CLI command options and flags

    main

    The Mako CLI supports the following arguments and options for controlling template rendering:

    OptionArgumentDescription
    --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 from stdin.
    mako [input] [--var name=value] [--template-dir dir] [--output-encoding encoding] [--output-file file]
  4. Use the Mako CLI to render templates

    main

    The 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 to stdout by default, but can be redirected to a file using the --output-file flag. You can also pass variables to the template using the --var flag.

    # 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