# set

## Purpose

Set the value of a variable accessible with the GSP page.

## Examples

    <g:set var="tomorrow" value="${new Date(today.getTime() + 24L * 60 * 60 * 1000)}" />
    <g:set var="counter" value="${1}" />
    <g:each in="${list}">
        ${counter}.&nbsp; ${it} -> ${counter % 2 == 0 ? 'even' : 'odd'}
        <g:set var="counter" value="${counter + 1}" /><br/>
    </g:each>

Using scopes:

    <g:set var="foo" value="${new Date()}" scope="page" />
    <g:set var="bar" value="${new Date()-7}" scope="session" />

Using the body contents:

    <g:set var="foo">Hello!</g:set>

## Description

Attributes

- `var` - The name of the variable
- `value` - The initial value to be assgined
- `scope` - Scope to set variable in (either `request`, `page`, `flash` or `session`. Defaults to `page`).

## Source

Show Source

    def set = { attrs, body ->
            def var = attrs.var
            if (!var) throw new IllegalArgumentException("[var] attribute must be specified to for <g:set>!")        def scope = attrs.scope ? SCOPES[attrs.scope] : 'pageScope'
            if (!scope) throw new IllegalArgumentException("Invalid [scope] attribute for tag <g:set>!")        def value = attrs.value
            def containsValue = attrs.containsKey('value')        if (!containsValue && body) value = body()        this."$scope"."$var" = value
            null
        }