Persona Viewtool
The $personatool is a Velocity ViewTool that provides access to persona-related functionality within dotCMS templates. It allows developers to retrieve, manage, and force personas for visitors, enabling personalized content delivery.
The following example shows how the persona tool is mapped in the toolbox.xml file:
<tool> <key>personatool</key> <scope>request</scope> <class>com.dotcms.rendering.velocity.viewtools.PersonaTool</class> </tool>
Methods#
| Method | Parameters | Return Type | Description |
|---|---|---|---|
forcePersona | String id | Persona | Forces a specific persona onto the current visitor by persona ID. Returns null if persona not found or ID is empty. Removes persona from visitor if not found. |
find | String id | Persona | Finds and returns a persona by ID with permission checking. Returns null if ID is empty. |
getPersonas | None | List<Persona> | Retrieves all personas available on the current host and system host (global personas). Returns null on error. |
Usage Examples#
Example 1: Getting All Available Personas#
## Retrieve all personas for the current site #set($personas = $personatool.getPersonas()) #if($personas && $personas.size() > 0) <h3>Available Personas:</h3> <ul> #foreach($persona in $personas) <li>$persona.name</li> #end </ul> #end
Example 2: Finding a Specific Persona#
## Find a persona by ID #set($personaId = "123e4567-e89b-12d3-a456-426614174000") #set($persona = $personatool.find($personaId)) #if($persona) <h2>Persona Details</h2> <p>Name: $persona.name</p> <p>Key Tag: $persona.keyTag</p> #end
Example 3: Forcing a Persona for Testing#
## Force a specific persona for the current visitor (useful for testing/preview) #set($testPersonaId = "marketing-director-persona") #set($forcedPersona = $personatool.forcePersona($testPersonaId)) #if($forcedPersona) <div class="persona-notice"> Now viewing as: $forcedPersona.name </div> #else <div class="error"> Unable to force persona. It may not exist or you lack permissions. </div> #end
Example 4: Conditional Content Based on Persona#
## Get all personas and check if a specific one is active #set($personas = $personatool.getPersonas()) #set($targetPersona = $personatool.find("developer-persona")) #if($targetPersona) ## Show developer-specific content <div class="developer-content"> <h2>Technical Documentation</h2> <p>Advanced API references and code examples...</p> </div> #end
Notes#
- The tool respects dotCMS permissions and only returns personas the current user has access to view
- When
forcePersona()is called with an invalid ID, it removes any existing persona from the visitor - All persona operations are tied to the current request context
- Global personas (from system host) are included in
getPersonas()results alongside site-specific personas - Error conditions return
nullrather than throwing exceptions to the template layer