A introduction to the CONTENT object

 / Main / Articles / Explaining the CONTENT object

The CONTENT object is in TYPO3 used to read something form a database table. It is especially usefull if you want to format the content in a special way, eg. as parameters for a flash movie.

 

In the following article i will try to explain with examples the basics of the CONTENT object, but i will also have a look at some more advanced possibilities.

 

Lets start with a simple example:

Read max. 10 contents from the right column of the current page. If there is a image show it too.

 

temp.right_content=CONTENT
temp.right_content{
    # The table from which is beeing read ( the only one allowed are "pages" or tables with the prefix "tt_","tx_","ttx_","fe_" or "user_")
  table=tt_content
    # wrap the whole element
  wrap=|
    # the SQL-Query
  select{
        # Set the language field
    languageField=sys_language_uid
        # limit 10 entries
    max=10
        # selectFields is optional, the default value is *. When looking at perfomance i recomand to set the used fields here.
    selectFields=image,header
     
        # The where clausel, in this case colPos=2 for the right column
    where=colPos=2
  }
  renderObj=COA
  renderObj{
        # create a IMAGE object
    5=IMAGE
    5{
            # only output it if not empty
      required=1
      wrap=|
      file.import=uploads/pics/
      file.import.field=image
      file.width=100
      file.height=100
    }
        # The header of the content element
    10=TEXT  
    10{
      required=1
      wrap=|
      field=header
    }
        # Following TEXT ement would output all the available fields, that means if you dont set selectFields all of the available database fields are outputted
        #20=TEXT
        #20.data = debug:data
  }
}

 

Substantially the CONTENT element contains 2 parts: the select part and the renderObj. In the select part the SQL-query is defined and in the renderObj you define which fields are how formatted.

The most flexible way is to define the renderObj as a COA, because there you can insert as many other cObjects (TEXT,IMAGE,..) as you want.

 

CONTENT objects can also be nested, just define in the renderObj a other CONTENT object. This second CONTENT object can now access the fields selected with CONTENT element before.

 

Following sample prints the header of the page, reads its subpages and prints the first content element with image and creates a link to the page. Also the bodytext is being outputted and cutted after 20 chars.

 

temp.sectionMenu=COA
temp.sectionMenu{
  5=TEXT
    # Title of the page
  5.data=leveltitle:-2
  5.wrap=
  <h1>|</h1>
    # first CONTENT object for the header
  10=CONTENT
  10{
    stdWrap.wrap=|
    table=pages
    select.orderBy=sorting
           # If this element is inserted as sitemap, consider the startingpoint
    select.pidInList.if.isTrue.field=pages
    select.pidInList.field=pages
    renderObj=COA
    renderObj{
      stdWrap.wrap=|
       
        		# Title and link to the page
      5=TEXT
      5.wrap=<b>|</b>
      5.field=title
      5.typolink.parameter.field=uid
        		# 2. CONTENT object for reading the first content element
      10=CONTENT
      10{
        table=tt_content
        select.pidInList.field=uid
        select.where=colPos=0
        select.orderBy=sorting
        select.max=1
        renderObj=COA
         
          			#read the image
        renderObj.2=IMAGE
        renderObj.2{
          wrap=|
            				# show it only if inserted
          stdWrap.if.isTrue.field=image
          stdWrap.typolink.parameter.field=pid
          file.import=uploads/pics/
          file.import.field=image
          file.width=100
          file.height=100
        }
         
          			#read the bodytext
        renderObj.10=TEXT
        renderObj.10{
          field=bodytext
            				#cut after 20 chars, keep whole words if possible
          crop=20 | ... |1
        }
      }
        		# more link
      20=TEXT
      20.wrap=<b>|</b>
      20.value=[more]
      20.typolink.parameter.field=uid
    }	
  }
}

OptionSplit and CONTENT objects

We all know optionSplit from menues, but how is this applied to CONTENT objects?

 

If we are looking in the TSREF we see that a lot of menu properties have a +opionSplit, but no properties in other objects have it.

 

This can be solve with the split function.

Just append to every element that should be outputted with optionSplit a placeholder, and afterward replace this placeholder with split.

 

Following samples prints the header and the text of the content elements of the actual page in the following form:

 

<div class="first">first </div>
<div class="second">second</div>
<div class="third">third</div>
<div class="middle1">middle 1</div>
<div class="middle2">middle 2</div>
<div class="middle1">middle 1</div>
<div class="middle2">middle 2</div>
<div class="last">last one</div>

 

 

Typoscript:

 

#Create the CONTENT object
temp.contentToSplit=CONTENT
temp.contentToSplit{
  table=tt_content
  renderObj=COA
  renderObj{
        # Append to every element ###SPLITTER### 
    stdWrap.outerWrap = |###SPLITTER###
    10=TEXT
    10.field=header
    10.wrap=|<br>
    20=TEXT
    20.field=bodytext
    20.wrap=|<br>
  }
}

temp.content = COA
temp.content {
     # Copy the readet content here
  10 < temp.contentToSplit
  stdWrap.split {
        # replace ###SPLITTER### with the split option
    token = ###SPLITTER###
        # the order is beeing defined in the normal optionSplit style
    cObjNum = 1 || 2 || 3 |*| 4 || 5 |*| 6 || 7
        # define the wraps on every position
    1.current = 1
    1.wrap = <div class="first">|</div>
    2.current = 1
    2.wrap = <div class="second">|</div>
    3.current = 1
    3.wrap = <div class="third">|</div>
    4.current = 1
    4.wrap = <div class="middle1">|</div>
    5.current = 1
    5.wrap = <div class="middle2">|</div>
    6.wrap = <div class="last">|</div>
    6.current = 1
        # leave last splitter empty
    7.current = 1
  }
}

 

 

You can apply this approach also with the normal styles.content.get

Sample:

 

#set the placeholder after everey content element
tt_content.stdWrap.outerWrap = |###SPLITTER###
temp.content = COA
temp.content {
  10 < styles.content.get
  stdWrap.split {
    token = ###SPLITTER###
    cObjNum = 1 || 2 || 3 |*| 4 || 5 |*| 6 || 7
    1.current = 1
    1.wrap = <div class="first">|</div>
    2.current = 1
    2.wrap = <div class="second">|</div>
    3.current = 1
    3.wrap = <div class="third">|</div>
    4.current = 1
    4.wrap = <div class="middle1">|</div>
    5.current = 1
    5.wrap = <div class="middle2">|</div>
    6.wrap = <div class="last">|</div>
    6.current = 1
    7.current = 1
  }
}

 

 

Samples:

Content Element Navigation

Content Element which shows also single elements

10 last changed contents

Random content

 

Official documentation:

TSREF CONTENT

TSREF select

TSREF split



Comments


Pablo, 06-12-11 16:35:
Hey

I'm doing a kind of paginator in TS...
I'm doing a select, but I need to confugre (from a GP:var) the begin value...
I dont find a way to do that...

It seems the begin is just a int... But I dont find a way to do this

Any Idea?

Thanks in Advance

Cheers
Peter, 22-04-12 10:03:
The last TS-snipped solved an issue for me, thanks for sharing this knowledge.

Add comment

* - required field

*



*
*

Top 10 Updates

Contact us geändert am  21-Jul-2013 11:33:07
Contact us geändert am  21-Jul-2013 11:33:03
Useful Links geändert am  24-Apr-2013 08:31:20
Nice fonts using Google font API geändert am  20-May-2010 16:36:47
TS-based Searchbox for Indexed Search  geändert am  18-May-2010 18:27:15
TYPO3 SEO geändert am  30-Apr-2010 09:44:22
Search engine friendly graphical headers in TYPO3 geändert am  30-Apr-2010 09:33:44
Creating a simple Infocenter using TYPO3 geändert am  17-Nov-2009 12:12:52
Making the Section Menu work correctly with TemplaVoila geändert am  05-May-2009 12:27:17
Show pages with type "Not in Menu" in a sitemap geändert am  05-May-2009 12:11:17

Latest comments

14-Mar-2014 15:53:15
Jesse Cain schrieb: I really like the backend of Typo3 and LDAP was easy and working great but I have checked what is...
My TYPO3 Page shows no content
11-Dec-2013 07:55:03
soma schrieb: Menu that recognizes when the user is on target page of the shortcut and wraps it with "active"...
External links and shortcuts directly in the menu
29-Sep-2013 21:09:40
Shirley Shirley schrieb: Hello,Thank you for this useful post.I don't find how to do the optoispe: remove item menu from the...
Alternativ sorting for menu items
Deutsch
Search: