<commit_before>Defs
=============

The def is the single tag used to demarcate any block of text and/or code.  It exists within generated Python as a callable function. 

    <%def name="hello()">
        hello world
    </%def>

They are normally called as expressions.

    the def:  ${hello()}

If the `<%def>` is not nested inside of another `<%def>`, its known as a **top level def** and can be accessed anywhere in the template, including above where it was defined.

All defs, top level or not, have access to the current contextual namespace in exactly the same way their containing template does.  Suppose the template below is executed with the variables `username` and `accountdata` inside the context:

    Hello there ${username}, how are ya.  Lets see what your account says:
    
    ${account()}

    <%def name="account()">
        Account for ${username}:<br/>
    
        % for row in accountdata:
            Value: ${row}<br/>
        % endfor
    </%def>

The `username` and `accountdata` variables are present within the main template body as well as the body of the `account()` def.

Since defs are just Python functions, you can define and pass arguments to them as well:

    ${account(accountname='john')}
    
    <%def name="account(accountname, type='regular')">
        account name: ${accountname}, type ${type}
    </%def>

When you declare an argument signature for your def, they are required following normal Python conventions (i.e., all arguments are required except keyword arguments with a default value).  This is in contrast to using context-level variables, which evaluate to `UNDEFINED` if you reference a name that does not exist.

### Calling defs from Other Files {@name=remotedefs}

Top level `<%defs>` are **exported** by your template's module, and can be called from the outside; including from other templates, as well as normal Python code.  Calling a `<%def>` from another template is something like using an `<%include>` - except you are calling a specific function within the template, not the whole template.

The remote `<%def>` call is also a little bit like calling functions from other modules in Python.  There is an "import" step to pull the names from another template into your own template; then the function or functions are available.

To import another template, use the `<%namespace>` tag:

    <%namespace name="mystuff" file="mystuff.html"/>

The above tag adds a local variable "mystuff" to the current scope.

Then, just call the defs off of `mystuff`:

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

The `<%namespace>` tag also supports some of the other semantics of Python's `import` statement, including pulling names into the local variable space, or using `*` to represent all names, using the `import` attribute:

    <%namespace file="mystuff.html" import="foo, bar"/>

This is just a quick intro to the concept of a **namespace**, which is a central Mako concept that has its own chapter in these docs.  For more detail and examples, see [namespaces](rel:namespaces).

### Defs within Defs {@name=nesteddefs}

The def model follows regular Python rules for closures.  Declaring `<%def>` inside another `<%def>` declares it within the parent's **enclosing scope**:

    <%def name="mydef()">
        <%def name="subdef()">
            a sub def
        </%def>
        
        im the def, and the subcomopnent is ${subdef()}
    </%def>

Just like Python, names that exist outside the inner `<%def>` exist inside it as well:

    <%
        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>

Assigning to a name inside of a def declares that name as local to the scope of that def (again, like Python itself).  This means the following code will raise an error:

    <%
        x = 10
    %>
    <%def name="somedef()">
        # error !
        somedef, x is ${x}  
        <%
            x = 27  
        %>
    </%def>

...because the assignment to `x` declares x as local to the scope of `somedef`, rendering the "outer" version unreachable in the expression that tries to render it.
    
### Calling a def with embedded content and/or other defs {@name=defswithcontent}

A flip-side to def within def is a def call with content.  This is where you call a def, and at the same time declare a block of content (or multiple blocks) that can be used by the def being called.  To achieve this, the target def is invoked using the `<%call>` tag instead of the normal `${}` syntax.  The target def then gets a variable `caller` placed in its context which contains a **namespace** containing the body and other defs defined within the `<%call>` tag.  The body itself is referenced by the method `body()`:

    <%def name="buildtable()">
        <table>
            <tr><td>
                ${caller.body()}
            </td></tr>
        </table>
    </%def>
    
    <%call expr="buildtable">
        I am the table body.
    </%call>
    
This produces the output:

    {html}
    <table>
        <tr><td>
            I am the table body.
        </td></tr>
    </table>

The `body()` can be executed multiple times or not at all.  This means you can use def-call-with-content to build iterators, conditionals, etc:

    <%def name="lister(count)">
        % for x in range(1,count):
            ${caller.body()}
        % endfor
    </%def>
    
    <%call expr="lister(3)">
        hi
    </%call>
    
Produces:
    
    {html}
    hi
    hi
    hi

A custom "conditional" tag:
    
    <%def name="conditional(expr)">
        % if expr:
            ${body()}
        % endif
    </%def>

    <%call expr="conditional(4==4)">
        im the result
    </%call>

Produces:

    {html}
    im the result

But that's not all.  The `body()` function also can handle arguments, which will augment the local namespace of the body callable:

    <%def name="layoutdata(somedata)">
        <table>
        % for item in somedata:
            <tr>
            % for col in item:
                <td>${body(col=col)}</td>\
            % endfor
            </tr>
        % endfor
        </table>
    </%def>
    
    <%call expr="layoutdata([[1,2,3],[4,5,6],[7,8,9]])" args="col">
        Body data: ${col}
    </%call>
    
Produces:

    {html}
    <table>
        <tr>
            <td>Body data: 1</td><td>Body data: 2</td><td>Body data: 3</td>
            <td>Body data: 2</td><td>Body data: 5</td><td>Body data: 6</td>
            <td>Body data: 3</td><td>Body data: 8</td><td>Body data: 9</td>
        </tr>
    </table>
    
You don't have to stick to calling just the `body()` function.  The caller can define any number of callables, allowing the `<%call>` tag to produce whole layouts:

    <%def name="layout()">
        # a layout def
        <div class="mainlayout">
            <div class="header">
                ${caller.header()}
            </div>
            <div class="sidebar">
                ${caller.sidebar()}
            </div>
            <div class="content">
                ${caller.body()}
            </div>
        </div>
    </%def>

    # calls the layout def
    <%call expr="layout">
        <%def name="header()">
            I am the header
        </%def>
        <%def name="sidebar()">
            <ul>
                <li>sidebar 1</li>
                <li>sidebar 2</li>
            </ul><commit_msg>correction<commit_after>Defs
=============

The def is the single tag used to demarcate any block of text and/or code.  It exists within generated Python as a callable function. 

    <%def name="hello()">
        hello world
    </%def>

They are normally called as expressions.

    the def:  ${hello()}

If the `<%def>` is not nested inside of another `<%def>`, its known as a **top level def** and can be accessed anywhere in the template, including above where it was defined.

All defs, top level or not, have access to the current contextual namespace in exactly the same way their containing template does.  Suppose the template below is executed with the variables `username` and `accountdata` inside the context:

    Hello there ${username}, how are ya.  Lets see what your account says:
    
    ${account()}

    <%def name="account()">
        Account for ${username}:<br/>
    
        % for row in accountdata:
            Value: ${row}<br/>
        % endfor
    </%def>

The `username` and `accountdata` variables are present within the main template body as well as the body of the `account()` def.

Since defs are just Python functions, you can define and pass arguments to them as well:

    ${account(accountname='john')}
    
    <%def name="account(accountname, type='regular')">
        account name: ${accountname}, type ${type}
    </%def>

When you declare an argument signature for your def, they are required following normal Python conventions (i.e., all arguments are required except keyword arguments with a default value).  This is in contrast to using context-level variables, which evaluate to `UNDEFINED` if you reference a name that does not exist.

### Calling defs from Other Files {@name=remotedefs}

Top level `<%defs>` are **exported** by your template's module, and can be called from the outside; including from other templates, as well as normal Python code.  Calling a `<%def>` from another template is something like using an `<%include>` - except you are calling a specific function within the template, not the whole template.

The remote `<%def>` call is also a little bit like calling functions from other modules in Python.  There is an "import" step to pull the names from another template into your own template; then the function or functions are available.

To import another template, use the `<%namespace>` tag:

    <%namespace name="mystuff" file="mystuff.html"/>

The above tag adds a local variable "mystuff" to the current scope.

Then, just call the defs off of `mystuff`:

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

The `<%namespace>` tag also supports some of the other semantics of Python's `import` statement, including pulling names into the local variable space, or using `*` to represent all names, using the `import` attribute:

    <%namespace file="mystuff.html" import="foo, bar"/>

This is just a quick intro to the concept of a **namespace**, which is a central Mako concept that has its own chapter in these docs.  For more detail and examples, see [namespaces](rel:namespaces).

### Defs within Defs {@name=nesteddefs}

The def model follows regular Python rules for closures.  Declaring `<%def>` inside another `<%def>` declares it within the parent's **enclosing scope**:

    <%def name="mydef()">
        <%def name="subdef()">
            a sub def
        </%def>
        
        im the def, and the subcomopnent is ${subdef()}
    </%def>

Just like Python, names that exist outside the inner `<%def>` exist inside it as well:

    <%
        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>

Assigning to a name inside of a def declares that name as local to the scope of that def (again, like Python itself).  This means the following code will raise an error:

    <%
        x = 10
    %>
    <%def name="somedef()">
        # error !
        somedef, x is ${x}  
        <%
            x = 27  
        %>
    </%def>

...because the assignment to `x` declares x as local to the scope of `somedef`, rendering the "outer" version unreachable in the expression that tries to render it.
    
### Calling a def with embedded content and/or other defs {@name=defswithcontent}

A flip-side to def within def is a def call with content.  This is where you call a def, and at the same time declare a block of content (or multiple blocks) that can be used by the def being called.  To achieve this, the target def is invoked using the `<%call>` tag instead of the normal `${}` syntax.  The target def then gets a variable `caller` placed in its context which contains a **namespace** containing the body and other defs defined within the `<%call>` tag.  The body itself is referenced by the method `body()`:

    <%def name="buildtable()">
        <table>
            <tr><td>
                ${caller.body()}
            </td></tr>
        </table>
    </%def>
    
    <%call expr="buildtable">
        I am the table body.
    </%call>
    
This produces the output:

    {html}
    <table>
        <tr><td>
            I am the table body.
        </td></tr>
    </table>

The `body()` can be executed multiple times or not at all.  This means you can use def-call-with-content to build iterators, conditionals, etc:

    <%def name="lister(count)">
        % for x in range(1,count):
            ${caller.body()}
        % endfor
    </%def>
    
    <%call expr="lister(3)">
        hi
    </%call>
    
Produces:
    
    {html}
    hi
    hi
    hi

A custom "conditional" tag:
    
    <%def name="conditional(expr)">
        % if expr:
            ${body()}
        % endif
    </%def>

    <%call expr="conditional(4==4)">
        im the result
    </%call>

Produces:

    {html}
    im the result

But that's not all.  The `body()` function also can handle arguments, which will augment the local namespace of the body callable:

    <%def name="layoutdata(somedata)">
        <table>
        % for item in somedata:
            <tr>
            % for col in item:
                <td>${body(col=col)}</td>\
            % endfor
            </tr>
        % endfor
        </table>
    </%def>
    
    <%call expr="layoutdata([[1,2,3],[4,5,6],[7,8,9]])" args="col">
        Body data: ${col}
    </%call>
    
Produces:

    {html}
    <table>
        <tr>
            <td>Body data: 1</td><td>Body data: 2</td><td>Body data: 3</td>
            <td>Body data: 4</td><td>Body data: 5</td><td>Body data: 6</td>
            <td>Body data: 7</td><td>Body data: 8</td><td>Body data: 9</td>
        </tr>
    </table>
    
You don't have to stick to calling just the `body()` function.  The caller can define any number of callables, allowing the `<%call>` tag to produce whole layouts:

    <%def name="layout()">
        # a layout def
        <div class="mainlayout">
            <div class="header">
                ${caller.header()}
            </div>
            <div class="sidebar">
                ${caller.sidebar()}
            </div>
            <div class="content">
                ${caller.body()}
            </div>
        </div>
    </%def>

    # calls the layout def
    <%call expr="layout">
        <%def name="header()">
            I am the header
        </%def>
        <%def name="sidebar()">
            <ul>
                <li>sidebar 1</li>
                <li>sidebar 2</li>
            </ul>
{'fraction_non_alphanumeric': 0.1200240851006891, 'fraction_numerical': 0.004549407907941393, 'mean_word_length': 2.4292268869006652, 'pattern_counts': {'":': 0, '<': 231, '<?xml version=': 0, '>': 231, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2457023', 'n_tokens_mistral': 4465, 'n_tokens_neox': 4172, 'n_words': 1835}