This documentation relates to an old version of VIVO, version 1.7.x. Looking for another version? See all documentation.
Since VIVO 1.6, it is possible for a Freemarker template to display data that is not normally provided to it.
You can create an RDF file that describes a custom
DataGetter object, and associates it with the desired template. Each time that template is used, the
DataGetter will be executed, and the data will be stored in a variable, so the template can display it.
This does not require changes to the Java code. You create the RDF file in your VIVO distribution directory and modify the template in your theme.
Let's assume that we need to display information about the most recent data ingest operation. We want to display the name of the Person who supervised the ingest. We would like to display this on every page.
As part of the ingest process, we can load statements like this into the data model:
We would like for VIVO to display the name of this individual on every page, so the footer will change from this:
Creating the DataGetter
VIVO allows you to define and use
DataGetter objects in several contexts.
DataGetters come in many flavors, but the most commonly used is the
SparqlQueryDataGetter, which lets you define a SPARQL query, and store the results of that query for your Freemarker template to display.
By adding statements to your data model, you can define a
SparqlQueryDataGetter object, and associate it with a Freemarker template. Here is the definition that is used in this example:
These statements can be added to your data model in any of several ways. For this example, I stored these lines in a file called
data_getter_for_example.n3 and placed it in the VIVO distribution directory under
rdf/display/everytime. Files placed in this directory are loaded when VIVO starts, but are not persisted when VIVO stops. This allows you to edit or remove the file without leaving residual statements in your data model.
- The first statement says that the Freemarker template
DataGetter, defined in subsequent lines.
- The definition of the
- the type of the data getter,
- the SPARQL query that will be executed
- the Freemarker variable that will hold the results.
The results of the SPARQL query are stored in a Freemarker variable, in this case
updatedInfo. The variable will contain a Sequence of Hashes, where each Hash represents one line of the SPARQL result. Within each Hash, result values are specified as key/value pairs.
For more information on Sequences and Hashes, consult the Freemarker manual:
Modifying the template
Here is the standard
footer.ftl template, as used in VIVO 1.6:
Insert these lines between lines 19 and 20:
The SPARQL result is obtained and stored into the Freemarker variable
updatedInfo each time the
footer.ftl template is loaded for display. The name we want is in the first row of the SPARQL result, keyed to the name
Enhancing Freemarker templates is one more way to use the VIVO
DataGetter mechanism. When you associate a
DataGetter with a Freemarker template, that
DataGetter will be run each time the template is invoked. This is true whether the template is specified by the controller, or included in another template. You can modify the template to display the data from the
DataGetter, but it is prudent to include an
<#if> tag, so your template won't fail if the data is not found.