Prev |  | Next

### `tal:define`

Defines variables in the template.

It takes one or multiple semicolon-separated variable definitions.

Making a shortcut to a long path:

                <div tal:define="destname path/to/existing/variable">…</div>

Defining more than one variable at the same time:

                <span tal:define="fname string:Paul; lname string:Dupond">…</span>

Each definition may start with `global` keyword which makes variable available everywhere later in the template and all macros. Global variables can be redefined.

                <span tal:define="global hello string:hello world"/><p tal:content="hello"/>

On the contrary, a local variable is only available inside the element it is defined in (and inside macros that are called in this element):

                <span tal:define="hello string:hello world"/><p tal:content="hello"/>
                <!-- will produce an undefined variable error -->

#### Tip

You may also use `tal:define` with other attributes, it will be executed before any other attributes on the same element.

Creating a string inside the template:

                <span tal:define="destname string:some string" />

Defining a string containing another variable:

                <span tal:define="fname string:Paul; hello string:Hello $fname! Welcome to this page" />

A small trick which uses content of the element (useful if you need to define very complex value):

                <span tal:define="global hello">Hello ${fname}! Welcome to this page</span>

In above example, the `<span>` tag won't show up because it has no printable content ( `hello` variable grabs it) nor attributes.

#### Note

This is a special case. It only works if you use the `global` keyword.

Prev | Up | Next
TAL namespace | Home | `tal:condition`