This page will collect useful snippets of Velocity templating code. As you build your own custom products, look here for ideas about how to access the information you need directly from the Velocity templates.
For general documentation on how to access configuraion information from the Velocity templates please see Getting configuration information from within an output template.
For more information on LAS java classes that are made available through the Velocity templates, please look at the LAS JavaDoc.
Accessing properties defined in the configuration file
Sometimes you want to add properties to the dataset configuration file and then have access to this information from within a Velocity template. This snippet uses methods of the Variable class to access and display all properties set up in the configuration file.
<ul>
## Get the list of data sets IDs for the variables
## that were in the request. There will be one data set ID
## for each variable, even if the variables are in the same
## data set.
#set($dsets = $las_request.datasetIDs)
## Loop over the data set IDs.
#foreach($id in $dsets)
## Get a Dataset config object for the dataset (this allows
## access to all its characteristics (like it name).
#set($ds = $las_config.getDataset($id))
## Put the name of the data set on the page.
<li>Data set: $ds.name
<ol>
## Use the data set ID to get the sibling
## variables
#foreach ($var in $las_config.getVariables($id))
<li> Variable: $var.name with ID = $var.ID</li>
## Get the HashMap of properties associated with each variable
#set ($properties_hash = $var.getProperties())
## Get the individual property groups
#set ($property_groups = $properties_hash.keySet().toArray())
<ul>
#foreach ($property_group in $property_groups)
<li>property_group = $property_group</li>
#set ($properties = $properties_hash.get($property_group))
#foreach ($property in $properties)
$property.name = $property.value,
#end
#end
</ul>
#end
</li>
#end
</ul>
Getting the Grid
If you're designing some smart, javascript interface that needs to know about the source grid associated with a particular dataset and variable you can use the following snippet to pass the JSON representation of the Grid to the LASGrid component. The LASGrid object then provides easy accessor methods for information about the grid. This is how the SlideSorter (aka comparison plot) works:
$LAS_HOME/WebContent/productserver/templates/SlideSorter.vm.
The following sinppet would be found within some javascript function defined in your template.
## The tepmlating language has no access to javascript variables
## so we must get dsID and varID the 'template way'
#set($dsID = $las_request.datasetIDs.get(0))
#set($varID = $las_request.variableIDs.get(0))
#set($grid = $las_config.getGrid($dsID,$varID))
#set($grid_JSON = $grid.toJSON().toString())
var gridJSON = '$grid_JSON';
// First, make sure we can parse the LASResponse
var Grid;
var JSONObject;
try {
var JSONObject = gridJSON.parseJSON();
} catch(e) {
alert('Error parsing gridJSON as JSON.');
return;
}
try {
Grid = new LASGrid(JSONObject);
} catch(e) {
alert(e);
return;
}