The model and the view

Webpagebytes CMS uses site pages as the Views concept from the MVC pattern.
The site pages can be interpreted as plain text (Source interpretation is Plain text source) or as template script based on Freemarker templating (Source interpretation is Template text source).

In case of Plain text source the site page is used as it is, without any interpretation.

When the source interpretation is Template text source then the site page content is interpreted as Freemarker script.

WPBModel class implements the model in the CMS scripts based on Freemarker. The model can be accessed in the controllers or in the Freemarker script.

Model structure

Besides the standard Freemarker script functionality, Webpagebytes CMS comes with a set of predefined model keys to provide the necessary data to generate rich content.

wpbAppModel
wpbGlobals
wpbUriParams
wpbPageParams
wpbRequest
wpbLocale
wpbMessages
wpbModule
wpbFormatText
wpbUri

wpbAppModel

The model key wpbAppModel can be used in the CMS Freemarker scripts to access the application specific data. This is the data populated by the controllers through WPBApplicationModel class.

// Example code from a controller implementation 
// adding 'userName' key with 'John' value as an application specific value
model.getCmsApplicationModel().put("userName", "John");
The following code is a freemarker script example of a site page
<html>
<body>
<#assign name=wpbAppModel["userName"] />
Hi ${name?html}!
This is a basic example.
</body>
</html>
The CMS engine will produce the following output
<html>
<body>
Hi John!
This is a basic example.
</body>
</html>

wpbGlobals

The model key wpbGlobals is used in the CMS Freemarker scripts to access the Global parameters . It will return a map of strings of strings.

Suppose we have a global parameter with the support phone number, named SUPPORT_PHONE_NUMBER with the value 787 787XXX.
<#assign supportNumber=wpbGlobals["SUPPORT_PHONE_NUMBER"] />
Do you need help? contact us at <b> ${supportNumber} </b>
The CMS engine will produce the following output
Do you need help? Contact us at <b> 787 787XXX </b>

wpbUriParams

The model key wpbUriParams is used in the CMS Freemarker scripts to access the site url customization parameters of the current HTTP request.

Suppose we have the following site url /sports/{article_id}-{seo_keywords}.html and two customzaition parameters article_id and keywords. A site page example can be
<html>
<body>
<#assign id=wpbUriParams["article_id"]!"" keywords=wpbUriParams["seo_keywords"]!"" />
The current article id is ${id?html} <br>
The current keywords are: ${keywords?html}
</body>
</html>
A HTTP request for /sports/2345-football-world-cup-2014.html will produce
<html>
<body>
The current article id is 2345 <br>
The current keywords are: football-world-cup-2014
</body>
</html>

wpbPageParams

The model key wpbPageParams is used in the CMS Freemarker scripts to access the site page customization parameters of the current HTTP request.

Suppose we have two site pages (home and about) and both of them have a customization parameter named 'menuSelection'. In one site page the parameter value is 'home' and in the other site page the parameter value is 'about'.
The next freemarker code is part of both site pages and simulates the main site menu navigation and marking the current menu tab with a specific css class 'active'.
<#assign menuSelection=wpbPageParams["menuSelection"]!"" />
<#assign homeCss="" aboutCss="" />
<#switch menuSelection>
    <#case "home">
         <#assign homeCss="active">
         <#break> 
    <#case "about">
         <#assign aboutCss="active">
         <#break> 
</#switch>
<ul class="menu">
	<li class="${homeCss}"> <a href="./home"> Home </a> </li>
	<li class="${aboutCss}"> <a href="./about"> About </a> </li>
</ul>
When home page will be requested it will produced the following content, see <li class="active">...
<ul class="menu">
	<li class="active"> <a href="./home"> Home </a> </li>
	<li class=""> <a href="./about"> About </a> </li>
</ul>
When about page will be requested it will produced the following content, see <li class="active">...
<ul class="menu">
	<li class=""> <a href="./home"> Home </a> </li>
	<li class="active"> <a href="./about"> About </a> </li>
</ul>

wpbRequest

The model key wpbRequest is used in the CMS Freemarker scripts to access the HTTP request protocol, domain and the base url.
wpbUriParams will return a map with the following keys:
WPB_GLOBAL_PROTOCOL
WPB_GLOBAL_DOMAIN
WPB_GLOBAL_CONTEXT_PATH
WPB_GLOBAL_BASE_URL

The Webpagebytes sample application is deployed into 'demo' context-path of the web server container. The following site page
<html>
<body>
protocol: ${wpbRequest["WPB_GLOBAL_PROTOCOL"]} <br>
domain: ${wpbRequest["WPB_GLOBAL_DOMAIN"]} <br>
context path: ${wpbRequest["WPB_GLOBAL_CONTEXT_PATH"]} <br>
base url: ${wpbRequest["WPB_GLOBAL_BASE_URL"]} <br>
</body>
</html>
will produce the following content
<html>
<body>
protocol: http <br>
domain: www.webpagebytes.com <br>
context path: /demo <br>
base url: http://www.webpagebytes.com/demo <br>
</body>
</html>

wpbLocale

The model key wpbLocale is used in the CMS Freemarker scripts to access the HTTP request locale (language and country).
wpbLocale will return a map with the following keys:
WPB_LOCALE_LANGUAGE
WPB_LOCALE_COUNTRY

<html>
<body>
language: ${wpbLocale["WPB_LOCALE_LANGUAGE"]} <br>
country: ${wpbLocale["WPB_LOCALE_COUNTRY"]}
</body>
</html>
Having a site url of /{language}-{country}/test and a HTTP request /en-GB/test will produce the following content
<html>
<body>
language: en <br>
country: GB
</body>
</html>

wpbMessages

The model key wpbMessages is used in the CMS Freemarker scripts to access the site messages corresponding to the current request locale.
The content generated can be localized with the wpbMessages values by enabling multiple languages for the project (see Locale settings section)

<html>
<body>
	This is message with 'key_1': ${wpbMessages["key_1"]} <br>
	This is message with 'key_2': ${wpbMessages["key_2"]} <br>	
</body>
</html>
Having the messages 'key_1' with value 'CMS example' and a message 'key_2' with value 'cloud integration' the above site page will produce the following output
<html>
<body>
	This is message with 'key_1': CMS example <br>
	This is message with 'key_2': cloud integration <br>	
</body>
</html>

wpbModule

This is a CMS Freemarker directive to include content from a site page module.

This example assumes there is a site page like the following
<html>
<body>
This is a site page module: <@wpbModule externalKey="17d13bb5-60fa-4efe-b4e7-ac989d6c70b1" /> 
</body>
</html>
The example assumes also that there is site page module with the following guid '17d13bb5-60fa-4efe-b4e7-ac989d6c70b1' and content
<b> content </b>
The CMS will produce the following content for the site page:
<html>
<body>
This is a site page module: <b> content </b>  
</body>
</html>
As can be seen the Freemarker directive <@wpbModule... was replaced with the page module content.

wpbFormatText

This is a CMS Freemarker directive helper for text formatting, it is based on Java™ Platform, Standard Edition MessageFormat class.

Example of Freemarker script that uses wpbFormatText utility.
${wpbFormatText("Deposit {0} by {1}.", "10$", "John")?html}
The CMS engine will generate
Deposit 10$ by John.

wpbUri

wpbUri is a CMS Freemarker directive that appends a special query parameter to a resource url to trigger the Cache-Control header and max-age directive.

<@wpbUri uriPattern="..." uriFile="..." > uri_path_here </@>

The script that uses wpbUri needs to specify one of the attributes uriPattern or uriFile.
uriPattern needs to match a site url. uriFile needs to match a site file path.

<@wpbUri uriPattern="/site.js">./site.js</@>
It will produce the output
./site.js?cqp=760291390