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:

Title:

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 | $content.name         | 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 | $content.localfolder.name<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)

end

####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**

/contentAsset/raw-data/${docContent.identifier}/[fieldVarName]/${docContent.diagram1.file}

**Binary Image: Resize Image**

/dA/$content.identifier/[fieldVarName]/filter/Resize/resize_w/500

**Thumbnail Image**

/dA/$content.identifier/[fieldVarName]/filter/Thumbnail/thumbnail_w/200/thumbnail_h/200/thumbnail_bg/255255255

####Associated File Fields

File metaData: $content.fileAsset.metaData

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

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

  $key : $meta.get($key)

end

File uri: $content.fileField.uri File inode: $content.fileField.map.inode File identifier: $content.fileField.map.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: $content.imageField.map.inode Image identifier: $content.imageField.map.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**

/dA/$content.imageField.map.identifier/fileAsset/filter/Resize/resize_w/500

####Fields with Pre-Defined Values

**Checkbox Field**

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

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

$value,

end

**Multi Select Field**

Options:

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

$option,

end

values: $content.multiSelectField.values Selected Values:

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

$value,

end

**Radio Field**

Options:

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

$option,

end

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

**Select Field**

Options:

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

$option,

end

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

####Taxonomy

**Category Field**

foreach($category in $content.categoryField)

$category.categoryName, $category.key

end

**Tags Field**

foreach($tag in $content.tagField)

$tag,

end

####Other

**Constant Field**

$content.constantField

**Custom Field**

$content.customField

**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
  #end

end

end