Confluence has a few distinct Velocity contexts for different purposes in the application (user macros, email templates, and exports), but the most commonly used context is called the "default context".
To detect deprecation and code change breakages during compilation time, it is recommended that, where possible, you add functionality that calls Confluence code in your plugin Java code (that is, actions or components) rather than in a Velocity template. Additionally, this makes your application more secure by not exposing complex objects on the Velocity context which can be leveraged by attackers in the event of an SSTI vulnerability.
You can call any method on your plugin action from Velocity
with $action.getComplicatedCustomObject()
instead of putting complicated logic in your Velocity template.
For example, if your plugin needs a calculated list of particular pages to display in the Velocity template, you should do the following:
PageManager
into your action class by using constructor
injection (more information on dependency injection).execute()
method, retrieve the desired pages using the pageManager
object and store them in a
field in your class called calculatedPages
.getCalculatedPages()
method to your action, which returns the list of pages.$action.calculatedPages
.You should NOT perform data updates or any mutations directly from Velocity templates and it will often be blocked; they should be used purely as a view layer.
The following list highlights the most important entries in the default Velocity context. You can get a full list by calling ConfluenceVelocityManager.getConfluenceVelocityContext(). The default Velocity context is used for templates rendered by:
Variable | Description |
---|---|
| The current Struts Action Class Reference: Struts Action class, usually a subclass of ConfluenceActionSupport |
|
Class Reference: I18NBean |
| Provides a date and time formatter suitable for the exporting user's locale and environment. Class Reference: DateFormatter |
| The current servlet request object (if available) Class Reference: HttpServletRequest |
| The current context path. Used for creating relative URLs:
Class Reference: String |
| The current servlet response object (avoid accessing in Velocity) Class Reference: HttpServletResponse |
| Current global settings configuration Class Reference: Settings |
| A general utility object containing miscellaneous helpful methods Class Reference: VelocityUtil |
| Provides methods for HTML and URL encoding Class Reference: HtmlUtil |
| The currently authenticated user, or Class Reference: ConfluenceUser |
| For retrieving users, groups and checking membership Class Reference: UserAccessor |
| Can be used to check permissions, but it is recommended that you check permission in your action Class Reference: PermissionHelper |
User macros have a Velocity context, which may include any of the above and some additional entries specific to the user macros. See Guide to user macro templates for a list of the latter.
In Confluence 7.13.15 LTS, 7.19.7 LTS, 8.2.0 and later versions, the user macro Velocity context is set to include only
the generalUtil
and htmlUtil
variables. To change or add more variables, you will need to
configure the system property macro.required.velocity.context.keys
.
See
configuring system properties to learn how.
Starting from Confluence 5.2, Soy templates are used instead of Velocity for email notifications.
When you customise the Velocity templates for Confluence email notifications, you can use the following items in addition to the default context.
Variable | Description | Class Reference |
---|---|---|
| Default stylesheet CSS contents | String |
| Same as | String |
| The email notification subject | String |
| Notification render context | |
| Daily report (only for digest notifications) | |
| Whether this notification should include diffs | boolean |
| Whether this notification should include full page content | boolean |
| Diff for the notification, if enabled |
The export context does not include any of the values from the default context. See Available Velocity contexts in exporters for a complete list.
Rate this page: