[TYPO3-core] RFC #17420: Add LOOP cObject

Xavier Perseguers typo3 at perseguers.ch
Mon Jan 31 16:48:38 CET 2011


Hi,

This is an SVN patch request.

Type: feature

Bugtracker reference:
http://bugs.typo3.org/view.php?id=17420

Branches: trunk

Problem:
TYPO3 is missing a content object to loop over arrays.

Solution:
Provide a LOOP content object to "loop" over an array of arbitrary data.


Remark:
If you have some collection that implements the Iterator interface, you 
should transform it into an array using PHP's built-in method:

iterator_to_array($yourIterator, TRUE)

otherwise tslib_content won't be happy as there is many places expecting 
->data to be an array.

Example of use:
in pi1/class.tx_yourext_pi1.php, method main():

$myData = array();
$myData[] = array('lastname' => 'Hader', 'firstname' => 'Oliver');
$myData[] = array('lastname' => 'Kamper', 'firstname' => 'Steffen');
$myData[] = array('lastname' => 'Baschny', 'firstname' => 'Ernesto');
$myData[] = array('lastname' => 'Perseguers', 'firstname' => 'Xavier');

$this->cObj->start($myData);
     return $this->cObj->cObjGetSingle(
     $this->conf['persons'],
     $this->conf['persons.']
);

in res/template.html:

<html>
<head>
     <title></title>
</head>
<body>

<!-- ###PERSONS### [begin] -->

<table>
     <thead>
         <tr>
             <th>###HEADER_FIRSTNAME###</th>
             <th>###HEADER_LASTNAME###</th>
         </tr>
     </thead>
     <tbody>
         <!-- ###PERSON### [begin] -->
         <tr>
             <td>###FIRSTNAME###</td>
             <td>###LASTNAME###</td>
         </tr>
         <!-- ###PERSON### [end] -->
     </tbody>
</table>

<!-- ###PERSONS### [end] -->

</body>
</html>

TypoScript:

plugin.tx_yourext_pi1.persons = TEMPLATE
plugin.tx_yourext_pi1.persons {
     template = FILE
     template.file = EXT:your_ext/res/template.html
     workOnSubpart = PERSONS

     marks {
         HEADER_FIRSTNAME = TEXT
         HEADER_FIRSTNAME.value = First Name
         HEADER_LASTNAME = TEXT
         HEADER_LASTNAME.value = Last Name
     }
     subparts.PERSON = LOOP
     subparts.PERSON.renderObj = TEMPLATE
     subparts.PERSON.renderObj.template < .template
     subparts.PERSON.renderObj {
         workOnSubpart = PERSON

         marks {
             FIRSTNAME = TEXT
             FIRSTNAME.field = firstname
             LASTNAME = TEXT
             LASTNAME.field = lastname
         }
     }
}

Cheers
Xavier
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 17420_v2.diff
Type: text/x-diff
Size: 5824 bytes
Desc: not available
URL: <http://lists.typo3.org/pipermail/typo3-team-core/attachments/20110131/39560d27/attachment-0001.diff>


More information about the TYPO3-team-core mailing list