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.
Method | Description |
---|---|
getHttpCookies() : Cookies | Returns the Cookies object, which can be used to manipulate the client-side cookies. |
getHttpHeaders() : Map | Returns a map containing all HTTP header values. |
isHttpSecure() : Boolean | Identifies if the request is secure. |
request.isSCAPI() : Boolean | Distinguishes 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.
Methods | Description |
---|---|
getCustomer() : Customer | Returns the current customer associated with this storefront session. For a Business Manager session, this method returns null. |
isCustomerAuthenticated() : Boolean | Identifies 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.
Methods | Description |
---|---|
static formatDate(date : Date) : String | Formats a date with the default date format of the current site. |
static formatInteger(number : Number) : String | Returns 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) : String | Returns a formatted number using the default number format of the current site. |
static garble(str : String, replaceChar : String, suffixLength : Number) : String | Return 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) : String | Provides cell padding functionality to the template. |
static stringToHtml(str : String) : String | Converts 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) : String | Converts 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) : String | Converts 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) : String | Returns the string with leading and trailing white space removed. |
static truncate(str : String, maxLength : Number, mode : String, suffix : String) : String | Truncates 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.
Method | Description |
---|---|
static abs(action : String, namesAndParams : String...) : URL | Returns an absolute URL with protocol and host from calling context request. |
static http(action : String, namesAndParams : String...) : URL | Returns 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...) : URL | Returns 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() : URL | Returns 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() : URL | Returns 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...) : URL | Returns a relative URL. |
static webRoot() : URL | Returns 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. |