Content Object in Velocity documentation for the dotCMS Content Management System

You may access the field values of any piece of content from Velocity code. To access the value of any Content field:

  1. Get a handle to the Velocity object for the piece of Content.
  2. Access the field as a property of the Content object, using the variable name assigned to the field in the Content Type.
    • Note: The property name is case-sensitive, so the case of the property name must exactly match the case of the variable name in the Content Type.

Parameter Display Example

The following code displays the “Title” and “Text Field” fields from a Content item by first getting the Velocity object representing the item, then referencing properties of the Velocity object which match the variable names of the Content Type fields.

#set($content = $dotcontent.find("4d7daefa-6adb-4b76-896d-c2d9f95b2280"))
Title:      $content.title
Text Field: $content.textField

Embedding HTML and Velocity in Content Fields

By default, all properties of Content objects retrieved through Velocity code are interpreted by both the HTML and Velocity parsers, allowing you to embed HTML formattting and Velocity code within Content item fields.

For example, consider a Content item with the following field values:


About Us

Text Field:

#set($content = $dotcontent.find("4d7daefa-6adb-4b76-896d-c2d9f95b2280"))
<b>Welcome</b> to the <i>$content.title</i> page!

The value of $content.title in the Text Field is automatically interpreted as Velocity code, so the following text will be displayed:

Welcome to the About Us page!

getRaw Method

Since Velocity code in Content fields is interpreted, if the fields are intended to display characters which have special meaning in Velocity, the fields may not display as expected. To prevent this, you may use the getRaw() method to stop the field values from being interpreted. To use this method, replace the name of the field variable (e.g. $content.textField) with .getRaw(), passing the name of the variable as a string to the getRaw method (e.g. $content.getRaw("textField")), as in the following examples:

Contents of the Text Field:

This tutorial will show you how to use a #define directive in a C program.

When accessed directly using the Content object variable (e.g. $content.textField), the #define will be interpreted as Velocity code, so the text in the field will not display correctly. (In fact the page may not display at all, since the “#define” in the text field is not properly formed Velocity code, so the Velocity parser will treat it as an error).

However, if the getRaw() method is used instead (e.g. $content.getRaw("textField")), the entire field will be passed through without interpretation, so the #define will not cause any problems and the field and page will both display correctly.

Content Properties

The following examples show how to access the values of different types of Content fields in your content objects. The examples assume you have created a content type with the properties below and have pulled one content object for that type referenced by the variable $content.

Please see the Pulling & Displaying Content documentation for more information on how to retrieve content from the Content Repository.

Basic Properties

The following properties of the content object are available for all content, of all Content Types.

#set($content = $dotcontent.find("4d7daefa-6adb-4b76-896d-c2d9f95b2280"))
Inode:      | String | $content.inode
Identifier: | String | $content.identifier
title:      | String | $content.title (this will be the first text element marked "show on menu").

URL Title:  $content.urlTitle

#### Content Type Fields {#FieldProperties}

Each field of a Content Type may be accessed by referencing the Velocity variable name of that field within the Content Type.  For most field types, the value of the field may be referenced directly.  Referencing some field types will return a Velocity object rather than a simple value, and you can then access separate properties of the object (for an example, please see [Host/Folder Field](), below).

Consider the following Content Type:

| Field Name  | Field Type     | Velocity Variable Name |
| Name        | Text           | name
| Description | Textarea       | description
| Story       | WYSIWYG        | story
| LocalFolder | Site or Folder | localfolder

| Text Field  | String | $         | Contents of the Text field (plain text)
| Textarea    | String | $content.description  | Contents of the Textarea field (plain text)
| WYSIWYG     | String | $content.story        | Contents of the WYSIWYG field, in HTML format
| Host/Folder | Object | $<br/>$content.localfolder.title<br/>$content.localfolder.identifier | Plain Text<br/>Plain Text<br/>Plain Text

####File Assets

set($file = $dotcontent.find(“4d7daefa-6adb-4b76-896d-c2d9f95b2280”))

uri: $file.fileAsset.uri inode: $file.fileAsset.inode identifier: $file.fileAsset.identifier path: $file.fileAsset.path mimeType: $file.fileAsset.mimeType extension: $file.fileAsset.extension fileSize: $file.fileAsset.fileSize width: $file.fileAsset.width height: $file.fileAsset.height metaData: $file.fileAsset.metaData

set($meta = $json.generate($file.fileAsset.metaData))

foreach($key in ${meta.keys()})

  $key : $meta.get($key)


####Date and Time

Date: $content.dateField –> $date.format(“MM/dd/yyyy”,$content.dateField) Date and Time: $content.dateAndTimeField –> $date.format(“MM/dd/yyyy hh:mm:ss z”,$content.dateAndTimeField) Time: $content.timeField –> $date.format(“hh:mm:ss z”,$content.timeField)

####Binary Field and Files

**Download Raw File**


**Binary Image: Resize Image**


**Thumbnail Image**


####Associated File Fields

File metaData: $content.fileAsset.metaData

set($meta = $json.generate($content.fileAsset.metaData))

foreach($key in ${meta.keys()})

  $key : $meta.get($key)


File uri: $content.fileField.uri File inode: $ File identifier: $ File size: $content.fileField.fileSize File mimeType: $content.fileField.mimeType File extension: $content.fileField.extension

####Associated Image Fields

Image metaData: $content.imageField.metaData Image uri: $content.imageField.uri Image path: $content.imageField.path Image inode: $ Image identifier: $ Image size: $content.imageField.fileSize Image height: $content.imageField.height Image width: $content.imageField.width Image mimeType: $content.imageField.mimeType Image extension: $content.imageField.extension

**Resized image**


####Fields with Pre-Defined Values

**Checkbox Field**

Options: $content.checkboxField.options values: $content.checkboxField.values Selected Values:

foreach($value in $content.checkboxField.selectedValues)



**Multi Select Field**


foreach($option in $content.multiSelectField.options)



values: $content.multiSelectField.values Selected Values:

foreach($value in $content.multiSelectField.selectedValues)



**Radio Field**


foreach($option in $content.radioField.options)



values: $content.radioField.values Selected Values: $content.radioField.selectValue

**Select Field**


foreach($option in $content.selectField.options)



values: $content.selectField.values Selected Values: $content.selectField.selectValue


**Category Field**

foreach($category in $content.categoryField)

$category.categoryName, $category.key


**Tags Field**

foreach($tag in $content.tagField)




**Constant Field**


**Custom Field**


**Key/Value Field**

if ($UtilMethods.isSet($content.keyValueField))

foreach($key in $content.keyValueField.entrySet())

  #if($key.key != "map" && $key.key != "keys")
     $key.key: $key.value