////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

Variables can be defined within a GSP using the xref:../../../../ref/Tags%20-%20GSP/set.adoc[set] tag:

[source,xml]
----
<g:set var="now" value="${new Date()}" />
----

Here we assign a variable called `now` to the result of a GSP expression (which simply constructs a new `java.util.Date` instance). You can also use the body of the `<g:set>` tag to define a variable:

[source,xml]
----
<g:set var="myHTML">
   Some re-usable code on: ${new Date()}
</g:set>
----

The assigned value can also be a bean from the applicationContext:

[source,xml]
----
<g:set var="bookService" bean="bookService" />
----

Variables can also be placed in one of the following scopes:

* `page` - Scoped to the current page (default)
* `request` - Scoped to the current request
* `flash` - Placed within xref:../../../../ref/Controllers/flash.adoc[flash] scope and hence available for the next request
* `session` - Scoped for the user session
* `application` - Application-wide scope.

To specify the scope, use the `scope` attribute:

[source,xml]
----
<g:set var="now" value="${new Date()}" scope="request" />
----
