At the heart of any dotCMS site or instance is the content repository. When building sites, the content repository can be accessed and content can be reused singly by id or as dynamic lists by using content queries.
When displaying the resulting content or contents, the content object is presented to the developer as a ContentMap. The Object holds a reference to the values of the fields defined by the Content Type for the given content object (VelocityVariableName). This is less confusing than it sounds. Let's say a content has a field "My Title" with a VariableName myTitle. To access the value of stored in this field for a piece of content, you would use $contentMap.myTitle. This will work with any field on a content including Category, Tag, File, Image or Binary.
In the case of the Image, Binary, and File fields - $contentMap.myImage returns a special object to make it easy to get at different properties of the field. ie… $contentMap.myFile.uri or $contentMap.myImage.width. For Binary a BinaryMap Object is returned. For Image and File a FileMap object is returned. For a Category field a list of Categories is returned.
In addition to the file, image and category fields there are other fields which return helper objects for you. They are Tag, Host, Multi, Select, and Radio. On the host you can always do a $con.host.
NOTE: All these objects can be split to the screen when developing showing you what can be accessed. ie… $contentMap.myFile NOTE: All Queries passed to the $dotcontent tool will get the language, live, working, and deleted options set if you do not pass them in. For example in EDIT MODE +working:true would be appended unless you specifically pass in live or working. Same with deleted and the languageId Examples
To pull content consider the following example which passes a content query, limit and sort.
#foreach($con in $dotcontent.pull("+contentType:NewsItem",10,"modDate desc")) $con.headline #end
In this example headline is a field on the news. We can display the category on news within the loop this way. $con.newsType where newsType is a Category field. The output would be:
This shows an Array of Categories. We can then use a #foreach loop to iterate over them and display information about each category, such as the categoryName.
Comments, Radio, Select, Host, and Mult-select Fields
The Comments field is a Select Field. The Select, Radio, Multi Select and Checkbox all allow you to get at the possible options and values as well as the selected values. Again here the field is printed to the screen so you can see what is available to get at.
#foreach($con in $dotcontent.pull("+contentType:NewsItem",10,"modDate desc")) COMMENTS : $con.comments ##The host will return the actual contentMap of the host content. HOST : $con.host MULTI : $con.multi HOST FIELD : $con.contentHost CHECKBOX : $con.checkbox #end ##end of foreach
dotcontent.query VS dotcontent.pull
You can use the exact same syntax as the pull example replacing pull with query. This will return to you a List of ContentletSearch Objects. The Objects only have a reference to the Inode and Identifier of the search results. This is useful when you don't need to display data but only query the Content Repository to test if a query returns any results or the number of results.
#set($myList = $dotcontent.query("+contentType:NewsItem",10,"modDate desc")) #if($myList.size>0)...#end
The query returns to you a List of Objects with the inode and identifier represented as Strings.
Getting a Content Count
An easy way to get a count (as oppossed to the example above), would be to use the helper method count(String query) on the dotcontent tool. The count method returns an int
#if($dotcontent.count("+contentType:NewsItem" > 0) $content.title #end
Pulling One piece of Content by Identifier or Inode
You can also use the content tool to look up a piece of content by either its identifier or inode. Using the $dotcontent.find method rather than a query will be more performant, as the $dotcontent.find method pulls the correct inode from a cache, rather than hitting the index. Ths $dotcontent.find method takes either the inode or identifier of the content object. Consider the below example where a piece of content has an identifier of 48190c8c-42c4-46af-8d1a-0cd5db894797 and an inode of 98683629-e1eb-4b3f-a5a6-5a084c6b0209. Both would return the exact same content.
<strong>Find By Identifier</strong> $dotcontent.find('48190c8c-42c4-46af-8d1a-0cd5db894797') <strong>Find by inode</strong> $dotcontent.find('98683629-e1eb-4b3f-a5a6-5a084c6b0209')