The Website viewtool is a helper tool that helps to retrieve path elements — hosts, folders, or subfolders — for a site.
The following example shows how the WebsiteWebAPI is mapped in the toolbox.xml file:
<tool>
<key>website</key>
<scope>request</scope>
<class>com.dotcms.rendering.velocity.viewtools.WebsiteWebAPI</class>
</tool>
Velocity Methods
Method | Return Type |
---|---|
$website.getFolder() | Folder object |
$website.getSubFolders() | List of Folder objects |
$website.getHost() | Host object |
$website.getHostName() | String |
Get Folder
Takes a folder path and host to retrieve the folder as an object.
Argument | Type | Details |
---|---|---|
Folder Path | String | Path of folder to retrieve as object. |
Host ID | String | Identifier of the Site/Host containing the folder. |
Examples:
## $website.getFolder(String folderPath, String hostId)
$website.getFolder("/application/apivtl/search", "48190c8c-42c4-46af-8d1a-0cd5db894797")
Get Subfolders
Takes either a Folder object or a folder path and host id to retrieve an array of Folder objects.
Argument | Type | Details |
---|---|---|
Parent Folder | String or Folder object | Path or object of folder containing the child Folder objects to fetch. |
Host ID | String | Optional if Folder object is supplied as first argument. Identifier of the Site/Host containing the folder. |
Examples:
## $website.getSubFolders(String parentFolder, String hostId)
#set($subfolders1 = $website.getSubFolders("/application/apivtl", "48190c8c-42c4-46af-8d1a-0cd5db894797"))
#foreach($f in $subfolders1)
$f
#end
## $website.getSubFolders(Folder parentFolder)
#set($folder1 = $website.getFolder("/application/apivtl/search", "48190c8c-42c4-46af-8d1a-0cd5db894797"))
#set($subfolders2 = $website.getSubFolders($folder1))
#foreach($f in $subfolders2)
$f
#end
Get Host
Returns the Host/Site as an object.
Argument | Type | Details |
---|---|---|
Host ID | String | Identifier of the Site/Host. |
Examples:
## $website.getHost(String hostId)
#set($hostbody = $website.getHost("8a7d5e23-da1e-420a-b4f0-471e7da8ea2d"))
$hostbody
$hostbody.identifier
Get Hostname
Returns the Host/Site name as a string.
Argument | Type | Details |
---|---|---|
Host ID | String | Identifier of the Site/Host containing the folder. |
Examples:
## $website.getHostName(String hostId)
$website.getHostName("8a7d5e23-da1e-420a-b4f0-471e7da8ea2d")
Javascript Methods
The JavaScript implementation of this viewtool is much simpler, containing only one method at the time of writing. Additionally, this tool is accessed in custom JavaScript endpoints through the variable site
instead of $website
.
Method | Return Type |
---|---|
site.getCurrentSiteId() | String |
Get Current Site ID
Returns the Host/Site ID as a string.
Argument | Type | Details |
---|---|---|
Request | Request Object | Accepts request from context. |
Example:
const request = context.request;
const siteID = site.getCurrentSiteId(request);