--
PedroRio - 11 Oct 2012
XEO Components and Templating
Introduction
In order to improve the layout customization of existing applications, the concept of "templating" was added to (some) existing components and the creation of new "templatable" components is also allowed. This document explains all you need to know in order to use and create the new "templating" system.
In order to create a component (the old way) you need two classes:
- The component class
- The renderer class
The component class holds the properties of the component and the renderer is responsible for the HTML output. The rendering of a component is usually something like this:
_
w.startElement( DIV );
w.startElement( UL );
Iterator<UIComponent> it = component.getChildren().iterator();
while (it.hasNext()){
Menu m = (Menu) it.next();
w.startElement (LI);
w.write (m.getText());
w.endElement(LI);
}
w.endElement(UL);
w.endElement(DIV);
_
This meant that you had absolutely no chance of customizing the look and feel of a given component (apart from creating a new renderer for the said component component). With component templating the following scenario is possible when building a component:
- Component class (with properties)
- Component Renderer (can be optional)
- Component Template (a file declaring how the component is rendered)
The component template cound be something like
<div>
<ul>
<#list this.children as menu>
<li>${menu.text}</li>
</#list>
</ul>
</div>
The template now declares how the component is rendered and can be switched at runtime by defining a new template for same component. The template has access to "variables" which will be replaced by theri values at runtime. Modern components require some help of javascript and css, as such it's possible to include that type of content in your templates, you can find more information in the Javascript Section.
Template Engine
_
The template system used in XEO builds upon the
Freemaker Library which has its own template language. The manual for creating Freemarker
templates can be found here. If you're not familiar with template systems we recommend checking the
wikipedia page for a more general approach and the
Freemarker page for a more specific one. The main ideia behind templating engines is rendering content using a variable-replacing system, meaning that if you want to render a "Hello" message followed by a username, you would create a template like
Hello ${username}
And pass the variable "
username" to the template engine to get something like "Hello John" or "Hello Bill". There are a lot more things you can do inside a template (like iterating through a list, using if/else statements and a lot more (check the Freemarker manual for a full list)).
_
Template Syntax Examples
_
The following is a sample of the
ActionButton template component. Notice the use of the
<#if> directive to choose whether or not the component shoud render the HTML, the use of the clientId, id, width and label properties to generate the HTML divs/buttons
<#if !this.renderedOnClient>
<div id='${this.clientId}'>
<button id='${this.id}_btn' class='xwc-actionbutton' style='min-width;${this.width}px'>
${this.label}
</button>
</div>
</#if>
_
In the following example (extracted from the Tabs components) you can see the use of lists and a special directive (explained later in this document)
<#list this.children as tab>
<div id='${tab.id}' class='xwc-tab-content'>
</div>
</#list>
<@xvw_facet />
You can also use Javascript (explained later in this document) and generate Javascript dinamically (this examples generates a JQuery call to create a panel). This
<@xvw_script position='footer'>
$(function() {
$( '#${this.id}' )
<#if this.collapsible>
.collapsiblePanel(true);
<#else>
.collapsiblePanel(false);
</#if>
});
</@xvw_script>
_
Template Variables - What's available
_
At the heart of the template engine there are the variables. When using a template combined with an XEO component you have access to all the properties available in the component. Each component is exported (into its own template) as the
this variable and the properties of the component can be accessed like the following:
${this.propertyName}
For the Section component which has properties like visible, and title you can do:
${this.visible} or
${this.title}
You also have access to any method the component has that follows Java Beans convention. If the component has getCustomValue() method, you can use ${this.customValue} (even if customValue is not a property of the component)
When you're creating a new template for an existing component refer to the component's API to check which variables are available.
_
Generic Template Component
_
You may need to create a view and "use" a specific template inside that view, but not necessarily associated with a component. In order to suppor that situation, the
Template component was created. The component has no properties other than the standard ones provided by
XUIComponentBase, but it has an interesting feature: Any property that is declared in the XML definition, will be passed to the template as ${this.properties.PROPERTY_NAME} . The properties can be literals or expressions meaning you could have the following:
<xvw:template template='myCustomTemplate' customProperty='Word' fruit='#{viewBean.myValue}'/>
And a template like the following:
<div>
Hello ${this.properties.customProperties} my favorite fruit is ${this.properties.fruit}!
</div>
And a bean with:
public String getMyValue(){
return "Orange";
}
Which would render the following:
<div>
Hello Word my favorite fruit is Orange!
</div>
_
Where to place your templates:
When you create a template for a component and reference the template by its name using the
template property (now available in every component), XEO will search for the template in the following places:
- A directory named "templates" inside your default web app.
- The source code root directory
Imagine a component like the following:
<xvw:actionButton template='vanilla/myTemplate.ftl' />
The loader will search for the template in
"webapps/default/templates/vanilla/myTemplate.ftl" and, if it does not find the template, in
"src/vanilla/myTemplate.ftl", if the template is not found in any of the locations, an error is thrown.
_
Component Properties regarding Templates
The
XUIComponentBase class (the base class from which all components derive) was added two new properties (both Binding properties whose value can be retrieved from the bean):
The
template property allows you to reference the template to be used in the rendering of the component by it's location (the location of the file with the template content) see the previous section for more detail on how template loading is done.
The
templateContent property allows you to dinamically generate the template's content in the viewer bean a pass it to the component. You need to generate a compliant with the Freemarker template engine and return as a String to the component, like the following:
(In the Viewer)
<xvw:actionButton templateContent='#{viewBean.content}' />
(In the Bean)
public String getContent(){
return "Hello ${username}"
}
_
_
Special Directive Processing
There are special intructions that are available for use in a template. There instructions are:
- xvw_script (Javascript inclusion)
- xvw_css (CSS inclusion)
- xvw_header (Page Header manipulation
- xvw_facet (Children rendering)
The following sections explain each directive in detail.
_
XVW_Script : Including Javascript
_
If you need to include a Javascript file for use in a template, or add an inline call do a Javascript function, you need the XVW_Script directive. It allows you to include files and inline calls at the header or footer of the page. The XVW_Script directive has the following properties:
- position (footer / header / inline ) : The position where the script will be added (relative to the page, defaults to header)
- id : The id of the script (optional, defaults to a dinamically generated id). If two scripts share the same ID only the last one will be added to the page
- src : The path to the script to include relative to the root of the web application (if it's an inline call to a script, use the body of the directive)
Example of how to include a script in a template:
<@xvw_script position='header' id='jquery' src='javascript/jquery/jquery.min.1.8.2.js' /
>
Example of how to add a call to a javascript function in a template
<@xvw_script position='footer'>
alert(${this.id});
</@xvw_script>
_
XVW_CSS : Including CSS
_
If you need to include a CSS file for use in a template, or add a CSS style inline you need the XVW_CSS directive. It allows you to include files and inline styles at the header or footer of the page. The XVW_CSS directive has the following properties:
- position (footer / header / inline ) : The position where the css will be added (relative to the page, defaults to header)
- id : The id of the script (optional, defaults to a dinamically generated id). If two css share the same ID only the last one will be added to the page
- src : The path to the css to include relative to the root of the web application (if it's an inline style, use the body of the directive)
Example of how to include a CSS file in a template
<@xvw_css position='header' src='path/to/myStyles.css' /
>
Example of how to add inline styles in a template
<@xvw_css position='header'>
.myStyle{
color:red;
font-size:13px;
}
</@xvw_css
>
_
XVW_Header : Writting to the page header
If there's the need to add a special header to the page (like adding a meta tag to a page, or something like that) you can use the <@xvw_header directive />
Example of adding a meta tag
<@xvw_header>
<meta name="description" content="Awesome Description Here">
</@xvw_header>
_
XVW_Facet : Processing Component Children, integrally or partially
The xvw_facet directive is related to the processing of child elements in templates. Several components are "childless", meaning they don't have children to be rendered, they're it! While other components can have children and may require that their children be rendered in a special place inside their own template. The xvw_facet directive allows you to specify exactly where children are rendered, lets see an example of Section component:
<fieldset id='${this.id}' class='ui-widget ui-widget-content xwc-section'>
<legend style='ui-widget-header ui-corner-all xwc-legend'>
${this.label}
</legend>
<@xvw_facet />
</fieldset>
The section component is basically a fieldset wrapping the content of its children. As such, the template draws the
fieldset and
legend tags, and then instructs the template (through the
<@xvw_facet> directive) to render its children
inside the fieldset. If a section component has as children a set of rows, they'll be rendered inside the fieldset.
But what if you know you have only a specific set of children and you want to render them individually in specific places? Well The xvw_facet directive is able to help you again. If you need a component (let's assume the generic "template" component) with two children and in the template of that component you need to render those children in specific places then you need to declare the children of that component wrapped in a f:facet component and give the facet a name. That name will be used in the template (with the xvw_facet directive):
<xvw:template template='myTemplate.ftl'>
<f:facet name='button'>
<xvw:actionButton>
</f:facet>
<f:facet name='grid'>
<xvw:gridPanel >
</f:facet>
</xvw:template>
And a template like the following (see the use of the xvw_facet directive with the name property to render the specific child in that position) :
<div id=${this.id}>
<fieldset>
<@xvw_facet name='actionButton' />
</fieldset>
<ul>
<li>TESTE </li>
</ul>
<@xvw_facet name='grid' />
</div>
Each child must have a unique id (if you want to render them in differente places).
_
Adhoc Template Processing
_
If for some reason you need to process a template within your code you use can the class netgest.bo.xwc.components.template.util.CustomTemplateRenderer, which has two methods;
public static String processTemplateFile(String templateName, Map<String,Object> context);
public static String processTemplate(String templateContent, Map<String,Object> context);
The
processTemplateFile method receives the name of a template (same rules defined earlier apply to finding templates) and a Map with the mappings between variables and their values to be applied with the template.
The
processTemplate method receives the content of the template as a String and a Map with the variables, for instance you have the following code in your application:
public String processCustomActionAndGenerateHtml(){
// Business logic
String template = "Hello ${user} ";
Map<String,Object> map = new HashMap<String,Object>();
map.put("user","John");
String result = CustomTemplateRenderer.renderTemplate(template,map);
return result;
}
And the result would be
Hello John
_
XEO Components:
Three components were created to integrate individual objects, object lists and lovs with templates. The details for each component is the in the following sections:
Xeo Objects Syntax inside templates:
If you're familiar with XEO's Java API you know that in order to get the value of an attribute from an instance you need to do instance.getAttribute("name").getValueString(), inside the templates we've made things simpler and you can do simply ${instance.name}, and iterate a bridge, you do <#list instance.bridge as element>
The
XeoList component allows you to display a list of objects using a template. The component has the following properties
- boql : The boql expression to execute and retrieve the list of objects
- name : The name for the variable to put in the context, will be available as ${this.properties.NAME}
You can use the component as follows:
<xeo:xeoList name='myVar' boql='select Ebo_Perf' template='list.ftl'>
And the template list.ftl:
<ul>
<#list this.properties.myVar as menu>
<li><a href="none.html">${menu.username}</a></li>
</#list>
</ul>
This will generate a list with a link for each username in the application
The
XeoObject component allows you to associate a single instance with a template. It has the following properties
- boql : The boql expression to execute and retrieve the object (if more than one is retrieved only the first one is used)
- name : The name for the variable to put in the context, will be available as ${this.properties.NAME}
You can use the component like the following
<xeo:xeoObject boql="select Demo where name = John" name="var" template='site/menus.ftl' ></xeo:xeoObject>
And use a template like:
<div>
<span>${this.properties.var.name}</span>
<span>${this.properties.var.age}</span>
<ul>
<#list this.properties.var.bridge as menu>
<li>${menu.name}</li>
</#list>
</ul>
</div>
The
XeoLov component allows you to have access to a specific List of Values. It has the following properties:
- name : The name for the variable to put in the context, will be available as ${this.properties.NAME}
- lovName : The name of the lov to load
The variable ${this.properties.NAME} contains a list with all values, which can be used in a template like this:
<#list this.properties.NAME as item>
<li>${item.value} - ${item.description}</li>
</#list>
Each lov item has the
.value and
.description property representing, respectively, the getValue and getDescription methods available in the lovObject class
_
Error in Template Processing
_
Wrapper for XVWScripts
_
New Components