Utility Functions

B2C Commerce includes utility functions used frequently in templates.

Because you are working in a template, and templates use request as a top-level variable, you can immediately call methods of this object.

Top-level variables include pdict, out, request and session.

The following are some frequently used methods. See the B2C Commerce Script API documentation for the complete documentation.

MethodDescription
getHttpCookies() : CookiesReturns the Cookies object, which can be used to manipulate the client-side cookies.
getHttpHeaders() : MapReturns a map containing all HTTP header values.
isHttpSecure() : BooleanIdentifies if the request is secure.
request.isSCAPI()  : BooleanDistinguishes between OCAPI and SCAPI requests in extension points (hooks).

Because you are working in a template, and templates use session as a top-level variable, you can immediately call methods in this class.

MethodsDescription
getCustomer() : CustomerReturns the current customer associated with this storefront session. For a Business Manager session, this method returns null.
isCustomerAuthenticated() : BooleanIdentifies if the customer associated with this session is authenticated. This call is equivalent to customerisAuthenticated().

The Template Processor pre-imports dw.util.StringUtils, so the StringUtils class can be used within a template by its simple name.

MethodsDescription
static formatDate(date : Date) : StringFormats a date with the default date format of the current site.
static formatInteger(number : Number) : StringReturns a formatted integer number using the default integer format of the current site. The method can also be called to format a floating point as an integer.
static formatNumber(number : Number) : StringReturns a formatted number using the default number format of the current site.
static garble(str : String, replaceChar : String, suffixLength : Number) : StringReturn a string in which a specified number of characters in the suffix isn't changed, and the rest of the characters are replaced with the specified character.
static pad(str : String, width : Number) : StringProvides cell padding functionality to the template.
static stringToHtml(str : String) : StringConverts a given string to an HTML-safe string. This method substitutes characters that conflict with HTML syntax (<,>,&,") and characters that are beyond the ASCII chart (Unicode 160-255) to HTML4.0 named character entities.
static stringToWml(str : String) : StringConverts a given string to a WML-safe string. This method substitutes characters that conflict with WML syntax (<,>,&,',"$) to WML named entities.
static stringToXml(str : String) : StringConverts a given string to an XML-safe string. This method substitutes characters that conflict with HTML syntax (<,>,&,") and characters that are beyond the ASCII chart (Unicode 160-255) to HTML4.0 named character entities.
static trim(str : String) : StringReturns the string with leading and trailing white space removed.
static truncate(str : String, maxLength : Number, mode : String, suffix : String) : StringTruncates the string to the specified length using specified truncate mode. Optionally, appends the suffix to a truncated string.

The Template Processor pre-imports dw.web.URLUtils, so the URLUtils class can be used within a template by its simple name.

MethodDescription
static abs(action : String, namesAndParams : String...) : URLReturns an absolute URL with protocol and host from calling context request.
static http(action : String, namesAndParams : String...) : URLReturns an absolute URL with HTTP protocol. If an insecure host is specified in preferences, the returned URL includes that host. Otherwise, it uses the host from the calling context request.
static https(action : String, namesAndParms : String...) : URLReturns an absolute URL with HTTPS protocol. If an insecure host is specified in preferences, the returned URL includes that host. Otherwise, it uses the host from the calling context request.
static httpsWebRoot() : URLReturns an absolute web root URL with HTTPS protocol and host and domain information from calling context request. If a secure host is specified in preferences, the returned URL includes that host. Otherwise, it uses the host from the request.
static httpWebRoot() : URLReturns an absolute web root URL with HTTP protocol and host and domain information from calling context request. If an insecure host is specified in preferences, the returned URL includes that host. Otherwise, it uses the host from the request.
static url(action : String, namesAndParams : String...) : URLReturns a relative URL.
static webRoot() : URLReturns a relative web root URL. It uses a web root URL to access all static media context for the site. It can reference the actual media file by appending a relative path.