global - Apache FreeMarker Manual

Unsupported web browser - Use a modern browser to view this website!

- Home
- Manual
- Java API

- Contribute
- Report a Bug
- Download

Manual
Previous Next

# global

## Synopsis

    <#global name=value>
    or
    <#global name1=value1 name2=value2 ... nameN=valueN>
    or
    <#global name>
      capture this
    </#global>

Where:

- `name`: name of the variable. It is not expression. However, it can be written as a string literal, which is useful if the variable name contains reserved characters, for example `<#global "foo-bar" = 1>`. Note that this string literal does not expand interpolations (as `"${foo}"`).
- `=`: Assignment operator, which can also be one of the shorthand assignment operators (`++`, `+=`, etc.), just like with the `assign` directive,
- `value`: the value to store. Expression.

## Description

This directive is similar to `assign`, but the variable created will be visible in all namespaces, and will not be inside any namespace. Exactly as if you would create (or replace) a variable of the data-model. Hence, the variable is global. If a variable with the same name already exists in the data-model, it will be hidden by the variable created with this directive. If a variable with the same name already exists in the current namespace, that will hide the variable created with `global` directive.

For example, with `<#global x = 1>` you create a variable that is visible as `x` in all namespaces, unless another variable called `x` hides it (for example a variable what you have created as `<#assign x = 2>`). In this case, you can use special variable `globals`, like `${.globals.x}`. Note that with `globals` you see all globally accessible variables; not only the variables that were created with `global` directive, but also the variables of the data-model.

Note for custom JSP tag users: The set of variables created with this directive corresponds to the JSP page-scope. This means, that if a custom JSP tag wants to get a page-scope attribute (page-scope bean), a variable with the same name in the current namespace will not hide it from the viewpoint of the JSP tag.

Previous Next