Backoffice Framework API Documentation for HTML Templates

Using a third-party javascript framework may cause some problems with the script loading sequence. A developer should also be aware that a widget can be used multiple times in a single perspective which has its consequences. To deal with such situation, Backoffice Framework introduces a functionality of widget HTML templates. This article documents the possibilities and configurations of widget HTML templates.

Introduction

Any static content elements of widgets HTML may be inserted using templates. This feature may be useful for localization (putting a label key in a template rather than a real labeled text). Also, it is crucial not to duplicate identities of the elements in HTML – a developer still needs to be aware that a widget can be used several times in a single perspective, which may result in the identifiers duplication. In order to avoid that, it is recommended to use templates.

It may be useful in some cases to create a view template, that is, to make all the widgets look similar and only inject widget-specific code in appropriate places.

Templates are HTML-like files that are preprocessed by Backoffice Framework before being sent to a web browser. They look similar to regular HTML, they are placed in the same directory as regular view files, the only thing that differs templates from standard widgets is that they need a proper widget configuration.

Creating a Template

Backoffice Framework by default uses the Apache VelocityInformation published on non-SAP site engine to process templates. Each widget HTML file should be written according to the Velocity documentation. Still, it is possible to change the templates engine, but you need to be aware that it affects all the widgets and all the templates.

A widget view file should be put in the same directory as for standard widgets:

extension/backoffice/resources/widgets/widget_name

It should also have .html extension (for example, view.html). To enable template processing for a specified widget, the widget needs to have the isTemplate setting set to true.

The result of processing a template should be fully qualified and proper HTML code that is to be sent directly to a web browser.

Template Values

Each template engine allows to use some values that are to be put instead of the placeholders. There are some predefined values that are provided by the Backoffice Framework, yet you can write your own values providers to widen the template possibilities.

Backoffice Framework provides the following values out-of-the-box:

Value name

Type

Description

root

java.lang.String

Root of a widget view URL. It contains a webserver URL and a perspective

(that is, in http://some.domain.address:9001/backoffice/widgetClasspathResource/com.sample.widgets.somewidget/view.html it is: /backoffice)

widget

java.lang.String

Full id of the current widget

(that is, com.sample.widgets.somewidget)

path

java.lang.String

Path to the widget resource relative to the widget root

(that is, view.html)

id

java.lang.String

Identity of a widget container. It is a unique value for each widget instance and changes every session. It is a value that should be used to make all identities in the widget unique.

context

org.zkoss.zul.html

Context in which a template is resolved. Usually, it is an org.zkoss.zul.html component, in which the widget view is to be put.

settings

java.util.Map<java.lang.String, java.lang.Object>

All widget settings

properties

java.util.Map<java.lang.String, java.lang.Object>

All properties defined in the application (incl. project.properties, local.properties, etc.). Properties are not segmented and may be accessed only by their full key

(that is, $properties["some.property.key"])

sessionInfo

com.hybris.cockpitng.util.js.JsWidgetSessionDTO

Session info gathered by jsWidgetSessionInfoCollector bean

Supported attributes:

  • String currentUser - displays the name of the logged user
  • Boolean admin - indicates if the current user has admin privileges
  • Locale currentLocale - displays the language the user currently uses
  • Locale[] enabledDataLocales - displays enabled languages
  • AuthorityGroup activeAuthorityGroup - shows the active Authority Group
  • Map<String, Object> customAttributes - exposed session information object can be customized. All the non-standard properties are persisted in the customAttributes map (for details, see Backoffice Framework API Documentation for AngularJS )

labels.local

java.util.Map<java.lang.String, java.lang.Object>

All localized segmented labels that are defined for the widget in the following directory:

extension/backoffice/resources/widgets/widget_name/labels/labels.properties

labels.global

java.util.Map<java.lang.String, java.lang.Object>

All localized segmented labels that are defined for the whole perspective

Custom Values

If needed, some additional values can be passed to the template engine. To do this, a class implementing com.hybris.cockpitng.web.template.TemplateValuesProvider must be created and registered as a Spring bean. Then, register a new bean abstractTemplateValuesProvidersExtender with the property additionalValues extended by the previously created bean, that is:

extension/backoffice/src/com/example/extension/templates/SampleValuesProvider.java

package com.example.extension.templates;
 
import com.hybris.cockpitng.web.template.TemplateValuesProvider;
 
import java.util.Collections;
import java.util.Map;
 
public class SampleValuesProvider implements TemplateValuesProvider {
 
    @Override
    public Map<String, Object> provideTemplateValues(final Object context, final String templateId) {
        return Collections.singletonMap("SomeProperty", "SomeValue");
    }
 
}
extension/resources/extension-backoffice-spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <alias name="defaultSampleValuesProvider" alias="sampleValuesProvider" />
    <bean id="defaultSampleValuesProvider" class="com.example.extension.templates.SampleValuesProvider" />
 
    <bean parent="abstractTemplateValuesProvidersExtender" lazy-init="false" init-method="extend" destroy-method="clean">
        <property name="additionalValues">
            <list merge="true">
                <value>sampleValuesProvider</value>
            </list>
        </property>
    </bean>
 
</beans>

When registering a new values provider, be aware that this provider would be asked for the values for every template. It means that the values returned would become accessible for every template in the perspective.

Custom Template Engine

By default, Backoffice Framework uses the Apache Velocity to resolve templates. This template engine allows to use conditional snippets and loops, to read Java-object fields, to walk through maps, collections, etc. This engine can still be changed by registering the appropriate Spring bean. To do this, a class implementing com.hybris.cockpitng.web.template.TemplateEngine must be created and registered as a Spring bean called templateEngine. The template engine may also need a bean called templateResolver that is able to translate a template identity into the appropriate template contents. Backoffice Framework uses a full http URL up to the template file as the identity of the template resolver (in this case the widget view file, that is, widgetClasspathResource/com.example.widgets.somewidget/view.html) and by default the resolver reads this file and returns its contents.

Further Steps

To see the example of using templates in widgets written in third-party javascript frameworks, proceed to the Using HTML Templates in AngularJS Widgets.