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. Retrieve the content object for the content.
    • This can be done with any of the $dotcontent Viewtool pull methods. For more information, please see the Pulling and Displaying Content documentation.
  2. Access the field as a property of the content object.
    • The name of the property is the Velocity 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.

Simple Example

The following code displays the “Title” and “Text Field” fields from a Content item:

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

Embedding HTML 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.

Embedded HTML

For example, consider a content item with the “Title” field set to the value <i>About Us</i> (including the HTML <i> and </i> tags), and the following Velocity code used to display the title field:

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

The value of $content.title in the Title field is output exactly as it is stored, so the following HTML will be displayed (and sent to the user's browser):

<b>Welcome</b> to the <i>About Us</i> 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 Type Fields

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 is a simple type (such as String or Integer) and may be referenced directly. However some field types are returned as Velocity objects, which allows you to then access multiple separate properties of the object (for an example, please see Host/Folder Field, below).

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.

PropertyData TypeVelocity Example
InodeString$content.inode
IdentifierString$content.identifier
TitleString$content.title

Simple Field Types

The following types of Content Type fields return simple values when accessed as properties of the content object. These field values may be accessed and used directly in your Velocity and HTML code.

Field TypeData TypeVelocity Example
TextString<h2 class="$content.fieldVar">Example Heading</h2>
TextareaString<p>$content.fieldVar</p>
DateDateTime$date.format("MM/dd/yyyy", $content.fieldVar)
Date and TimeDateTime$date.format("MM/dd/yyyy hh:mm:ss z", $content.fieldVar)
TimeDateTime$date.format("hh:mm:ss z", $content.fieldVar)

Files and Images

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

Binary Field

Binary fields are returned as objects. You can not access the contents of the binary field directly, but you can use the identifier and other properties of the object to access the appropriate file using standard URLs.

Note that when using URLs to access binary field values, you must usually also know (and use) the Velocity variable name of the Content Type field in the URL. All of the following examples are built using a value of binary1 for the Velocity variable name.

Binary File
/contentAsset/raw-data/${content.identifier}/binary1/${content.binary1.file}
Binary Image

Binary fields containing images can be accessed and transformed using the /contentAsset and /dA URL formats. For more information, please see the Image Resizing and Processing documentation.

Resized Image

<img src="/contentAsset/image/$content.identifier/binary1/filter/Resize/resize_w/500">

Thumbnail Image

<img src="/dA/$content.identifier/binary1/filter/Thumbnail/thumbnail_w/200/thumbnail_h/200/thumbnail_bg/255255255">
Binary Field Properties

The following examples show the properties available for Binary fields, depending on whether the file stored is a simple file asset or an image. These values may be accessed by referencing the Binary field as a property of the content object, and the specific field below as a property of the binary field (e.g. $content.binary1.fileSize).

File Asset Properties

<p>Binary Field File Asset Properties:</p>
<ul>
    <li>File metaData: $content.binary1.metaData</li>
        <ul>
           #set($meta = $json.generate($content.binary1.metaData))
           #foreach($key in ${meta.keys()}) 
              <li>$key : $meta.get($key)</li>
           #end
        </ul>
    <li>File uri:        $content.binary1.uri</li>
    <li>File path:       $content.binary1.path</li>
    <li>File inode:      $content.binary1.map.inode</li>
    <li>File identifier: $content.binary1.map.identifier</li>
    <li>File size:       $content.binary1.fileSize</li>
    <li>File mimeType:   $content.binary1.mimeType</li>
    <li>File extension:  $content.binary1.extension</li>
</ul>

Image Properties

Images stored in Binary fields have all of the properties of files (see above), and the following additonal properties specific to images:

<p>Binary Field Image-Specific Properties:</p>
<ul>
    <li>Image height:     $content.binary1.height</li>
    <li>Image width:      $content.binary1.width</li>
</ul>

Fields with Pre-Defined Values

**Checkbox Field

<p>Checkbox Field Properties:</p>
<ul>
    <li>Options:         $content.fieldVar.options</li>
    <li>Possible Values: $content.fieldVar.values</li>
    <li>Selected Values:</li>
        <ul>
        #foreach($value in $content.fieldVar.selectedValues) 
           $value#if($foreach.hasNext()), #end
        #end
        </ul>
</ul>

Multi Select Field

Options: 
#foreach($option in $content.fieldVar.options) 
   $option#if($foreach.hasNext()), #end
#end
values: $content.fieldVar.values
Selected Values: 
#foreach($value in $content.fieldVar.selectedValues) 
   $value#if($foreach.hasNext()), #end
#end

Radio Field

Options: 
#foreach($option in $content.fieldVar.options) 
   $option#if($foreach.hasNext()), #end
#end
Possible Values:  $content.fieldVar.values
Selected Values:  $content.fieldVar.selectValue

Select Field

Options: 
#foreach($option in $content.fieldVar.options) 
   $option#if($foreach.hasNext()), #end
#end
Possible Values:  $content.fieldVar.values
Selected Values:  $content.fieldVar.selectValue

Taxonomy

Category Field

A Category field returns a list of Category objects:

#foreach($category in $content.catFieldVar)
   $category.categoryName, $category.key
#end

Tag Field

A Tag field returns a list of strings:

#foreach($tag in $content.tagFieldVar) 
   $tag, 
#end

Relationship Field

The value returned from a Relationship field varies based on the Cardinality of the Relationship:

CardinalityValue Returned
One to OneSingle content object
One to ManyList of content objects
Many to OneSingle content object
Many to ManyList of content objects
List of Content Objects (One to Many, Many to Many)
<p>Related Content:</p>
<ul>
    #foreach($object in $content.relationshipFieldVar) 
       <li>$object.title</li>
    #end
</ul>
Single Content Object (One to One, Many to One)

When a single content object is returned, the field value of that content object may be referenced directly, without the need to first get the content object:

<p>Related Content: $content.relationshipFieldVar.title</p>

Other

Constant Field

Constant field values are stored as text (similar to a Text field). You can retrieve the value of a Constant field as follows:

$content.fieldVar

Custom Field

Custom field values are stored as a large block of text (similar to a Textarea field). You can retrieve the value of a Custom field (but not the code) as follows:

$content.fieldVar

Key/Value Field

#if ($UtilMethods.isSet($content.fieldVar))
   #foreach($key in $content.fieldVar.entrySet())
      #if($key.key != "map" && $key.key != "keys")
         $key.key: $key.value
      #end
   #end
#end