cObject: Typoscript Content Objects

2007 January 1 | Filed in: Typoscript, cObjects, Articles Christopher Torgalson

A short introduction to one of the most important parts of Typoscript: the cObject.

What is a cObject Anyway?

A "cObject" is a "content object," and content objects are the building-blocks of TYPO3-powered websites. They are used in the output of TYPO3's default content elements such as login-boxes and menus, they are used to output custom content, and are even used to template entire websites. In fact, everything on this page is a cObject!

Think of a website as a collection of HTML documents, and of TYPO3 as a tool for managing and publishing those documents. In this context, Typoscript—or "TS"—has three main uses:

  1. to retrieve specific data,
  2. to specify the HTML markup for the retrieved data, and
  3. to process the retrieved data in some way.

For example, the following TS:

  1. ### Example 1: Basic cObject
  2. lib.articles.cObject.1 = HTML
  3. lib.articles.cObject.1{
  4.   value {
  5.     data = page:title
  6.     typolink {
  7.       parameter.data = page:uid
  8.       wrap = <h1> | </h1>
  9.     }
  10.     case = upper
  11.   }
  12. }

Download plain text version

...generates the following HTML:

HTML output
<h1><a href="articles/single/" >COBJECT: TYPOSCRIPT CONTENT OBJECTS</a></h1>

But Why is it so Verbose?

If you're unfamiliar with Typoscript, you might think that the TS code is very long compared to the HTML it creates since it's eleven lines long, but only creates one line of HTML!

However, it's important to keep in mind that along with marking-up the title to this page, we've potentially marked up the title of every page in the website. In other words, wiith relatively little code, we have:

  1. retrieved the title of the current page,
  2. converted the page title to uppercase,
  3. retrieved the URL of the current page,
  4. used the current page's URL to wrap the page title in a link,
  5. enclosed the link in an h1 element, and
  6. accomplished all of the above using generic, fully re-usable code!

The fact that our TS sample above solves a given problem in multiple locations shows how misleading it is to simply compare the length of the two samples—the particular HTML code shown above solves a problem for one page only (i.e. to display this page's title), while the Typoscript above solves an entire class of problems (i.e. to display the title of any page).

Comments

No comments

Add comment

*
*
* required field

www.typo3apprentice.com
A Bedlam Hotel project © 2007—2010

Report a problem with this page

Bookmark and Share