Templates
An Omniview Template is a parameterized collection of graphical objects that are used when creating views. Templates can be created using the base Omniview objects or can be built up from other templates. The default location for templates included with Omniview is at:
C:\ProgramData\Omniview\Server\<project_name>\Projects\Standard_Include\Template
Templates are stored in a JSON file with the .ovt
file extension.
Template Structure
Templates are separated into two sections:
"defaults":{}
- Parameter definitions are entered into this section
- Parameters are user defined variables that can be referenced by the objects in the content section of the template.
"content":{}
- The content consists of the graphical objects that will be displayed by the template.
- Templates can be normal Omniview objects or other templates
- The content section is a dictionary. This is different from the ViewObjects section in a view, which is a list of objects. A group should be used as a root object if multiple objects will be used.
{
"defaults":
{
"parameter1":"defaultValue1",
"parameter2":"defaultValue2"
},
"content":
{
"$type": "Button",
"Name": "{{parameter1}}_Button",
"Text": "{{parameter2}}"
}
}
The value stored in a parameter can be referenced by using {{<parameter_name>}}
. When the template is loaded at run time, the parameter value is substituted into the template where this name appears, and is then deserialized into a native object. It should be noted that the string does not get evaluated after substitution. The resulting string must be compatible with the property. Transitions should be used where the properties of an object are defined by a function or mathematical relationship.
A template can be referenced from a view using the following syntax:
{
"$type": "Template",
"source": "<path to template.ovt>",
"parameters": {
"parameter1":"value1",
"parameter2":"value2"
}
}
This creates an instance of the templated object. Any values not set in the "parameters"
property will use their default values.
Example
The example below shows a template for a radial bar graph with a single line. This uses and builds on the code from the example shown in the transforms radial bar graph example.
Template:
{
"defaults": {
"name": "RadialBarGraph",
"graph_diameter": 100,
"bar_foreground": "#FF0000",
"bar_background": "#FFFFFF",
"center_background": "#FFFFFF",
"border_color":"#000000",
"border_width":2,
"bar_width":20,
"xPos":0,
"yPos":0,
"input_expression":""
},
"content": {
"$type": "Group",
"Name": "{{name}}_group",
"X": "{{xPos}}",
"Y": "{{yPos}}",
"Children": [
{
"$type": "Ellipse",
"Height": "{{graph_diameter}}",
"Width": "{{graph_diameter}}",
"X": 0,
"Y": 0,
"BorderColor": "{{border_color}}",
"BorderThickness": "{{border_width}}",
"Fill": "{{bar_background}}"
},
{
"$type": "Polygon",
"Path": "0,0",
"Fill": "{{bar_foreground}}",
"Thickness": "{{border_width}}",
"X": 0,
"Y": 0,
"transforms": [
{
"property": "Path",
"source": "{{input_expression}}"
},
{
"property": "X",
"source": "{{graph_diameter}}/2"
},
{
"property": "Y",
"source": "{{graph_diameter}}/2"
}
]
},
{
"$type": "Ellipse",
"Height": 0,
"Width": 0,
"X": "{{bar_width}}",
"Y": "{{bar_width}}",
"BorderColor": "{{border_color}}",
"BorderThickness": "{{border_width}}",
"Fill": "{{center_background}}",
"transforms":[
{
"property":"Height",
"source":"{{graph_diameter}} - (2 * {{bar_width}})"
},
{
"property":"Width",
"source":"{{graph_diameter}} - (2 * {{bar_width}})"
}
]
}
]
}
}
Usage:
{
"$type": "Template",
"source": "RadialBar.ovt",
"parameters": {
"graph_diameter":130,
"xPos":450,
"yPos":350,
"bar_width":5,
"border_width":0,
"border_color":"#00000000",
"bar_foreground":"#FF8000",
"input_expression":"ExampleCirclePlot(130)"
}
},
{
"$type": "Template",
"source": "RadialBar.ovt",
"parameters": {
"xPos":465,
"yPos":365,
"bar_width":5,
"border_width":0,
"border_color":"#00000000",
"input_expression":"ExampleCirclePlot2(100)"
}
}
Two radial bar graphs are combined to visualize two values. Using templates significantly reduces the amount of work that needs to be done when an object will be reused. It also provides a single point for most changes.
Python Example
A template can be called in Python using a TemplateLoader object. To use the TemplateLoader, follow these steps:
- Initiate the TemplateLoader object,
- Resolve the template using the path and a dictionary of template parameters,
- Instantiate and display the template.
from Omniview.Python import *
parameters = {
"height":15,
"width":15,
"clickExpression":"import data_table\ndata_table.ToggleTickBox(this)"
}
tmp = TemplateLoader()
tickBox = tmp.Resolve("Standard_Include.Template.Common.CheckBox.ovt",parameters)
Omniview.Windows[0].Content.AddViewObject(tickBox.Instantiate())
Best Practices
Templates are intended to be reused across multiple projects. Making changes to a template that is used in other Views or Projects can cause software breaking issues. Major changes should include a revision number in the file name to reduce backwards compatibility issues.