When managing multiple websites it is sometimes necessary to record information specific to each site. Although values that are common to all sites can be added to the Host Content Type as additional fields, there are times when data is unique only to a few sites or different on every site. In these cases unique values that refer to specific sites are better stored in specific site variables.
Adding a Site Variable
To add a site variable to a site:
- Select the Control Panel -> Sites Tool from the navigation sidebar.
- Right-click on the site you wish to add a varable to, and select Edit Site variables.
- In the Site Variables dialog, click the Add New Site Variable button.
- Fill in values for each of the properties and then press the Save button.
- Flush the server cache.
- Since site variables are intentionally not saved with the site itself, it may be necessary to flush the server cache before you can access the new site variable from your Velocity code.
Properties
You must fill in all three properties in the Adding or Editing a Variable popup. The Name and Key fields restrict which characters may be used:
Property | Acceptable Characters | Description |
---|---|---|
Name | Any displayable characters, including spaces. | A label. Not used or accessible via code. |
Key | Alphanumeric and underscores only. | The key used to access the variable from code. |
Value | Any. | The value of the variable. |
Accessing Site Variables from Velocity Code
The value of a site variable can retrieved throught the built-in $host_variable
Velocity object. Individual site variables are accessible as properties of the $host_variable
object:
$host_variable.{key}
You must replace {key}
in this notation with the Key value you assigned to the appropriate site variable. For example, if you have created a site variable with a key of local_site_id
, you would access it as $host_variable.local_site_id
.
In certain cases, such as in a Velocity Script workflow sub-action, the site context must be loaded to ensure the $host_variable
object is defined.
To add $host_variable to the context add the following to the code above any uses of $host_variable
, replacing [SITE_ID]
with either the site identifier or the site key:
#parse("LIVE/[SITE_ID].site")
Referencing Site Variables using the Scripting API
To reference site variables using the Scripting API, ensure the site context is loaded, using the method specified above for a Velocity Script workflow actionlet.
This is likewise specified in the Site Variables section in the Scripting API documentation.
Checking for Unset Variables
The $host_variable
object will only be created if you have defined site variables on your site. Therefore, you should ensure that your code either uses appropriate Velocity notation to handle empty values (e.g. $!{host_variable.local_site_id}
), or checks for the existence of the variable before using it, as in the following code:
#if( $UtilMethods.isSet( $host_variable.local_site_id )
#set( $localId = $host_variable.local_site_id )
#else
#set( $localId = "defaultId" )
#end