2016-03-29 42 views
1

我將TypoScript CONTENT對象與流體模板組合在一起。結合TypoScript和流體:迭代?

在頁面模板:

<f:cObject typoscriptObjectPath="lib.myItem" /> 

在TS:

lib.myItem = CONTENT 
lib.myItem { 
    table = tt_content 
    select.where = colPos = 0 
    select.languageField = sys_language_uid 
    renderObj = FLUIDTEMPLATE 
    renderObj { 
    file = {$customContentTemplatePath}/Myfile.html 
    layoutRootPath = {$customContentLayoutPath} 
    partialRootPath = {$customContentPartialPath} 
    dataProcessing { 
     10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor 
     10.references.fieldName = image 
    } 
    } 
} 

在Myfile.html:

{namespace v=FluidTYPO3\Vhs\ViewHelpers} 

<div class="small-12 medium-6 large-4 columns"> 
    <f:for each="{files}" as="file"> 
     <v:media.image src="{file}" srcset="1200,900,600" srcsetDefault="600" alt="{file.alternative}" treatIdAsReference="1"/> 
    </f:for> 
    <div class="fp-ql-txt"> 
     {data.header} > 
    </div> 
</div> 

但現在我意識到,因爲模板是由應用每個內容元素的renderObj,我沒有訪問fluid的每個關於迭代的信息。所以,我不能做到這一點:

<f:for each="{data}" as="item" iteration="itemIterator"> 
    {itemIterator.cycle} 
    </f:for> 

,找出其中所呈現的項目,我們是...,因爲每個單元由renderObj單獨呈現的。

如何獲取有關renderObj產品的迭代信息?只有在TS和http://typo3-beispiel.net/index.php?id=9那些古老而可怕的櫃檯?

回答

2

你可以使自己的IteratorDataProcessor:

dataProcessing { 
    10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor 
    10 { 
     references.fieldName = image 
    } 
    20 = Vendor\MyExt\DataProcessing\IteratorProcessor 
} 

在流體你可以訪問你在你的數據處理器與例如設置的東西:

<?php 
namespace Vendor\MyExt\DataProcessing; 

use TYPO3\CMS\Core\SingletonInterface; 
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; 
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; 
use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException; 

/** 
* This data processor will keep track of how often he was called and whether it is an 
* even or odd number. 
*/ 
class IteratorProcessor implements DataProcessorInterface, SingletonInterface 
{ 
    /** 
    * @var int 
    */ 
    protected $count = 0; 

    /** 
    * Process data for multiple CEs and keep track of index 
    * 
    * @param ContentObjectRenderer $cObj The content object renderer, which contains data of the content element 
    * @param array $contentObjectConfiguration The configuration of Content Object 
    * @param array $processorConfiguration The configuration of this processor 
    * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View) 
    * @return array the processed data as key/value store 
    * @throws ContentRenderingException 
    */ 
    public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) 
    { 
     $iterator = []; 
     $iterator['index'] = $this->count; 
     $iterator['isFirst'] = $this->count === 0; 
     $this->count++; 
     $iterator['cycle'] = $this->count; 
     $iterator['isEven'] = $this->count % 2 === 0; 
     $iterator['isOdd'] = !$iterator['isEven']; 
     $processedData['iterator'] = $iterator; 
     return $processedData; 
    } 
} 

在Typo腳本您通過處理器將您的數據{iterator.isFirst}

+0

哇,謝謝...當我有一分鐘時,我會嘗試一下 – Urs

+0

我想你不只是寫那個專案 - 你是否也有這個用例呢?你認爲這是一個有效的場景嗎? – Urs

+0

當然是。我將在我的一個項目中使用那個非常DataProcessor。 – Daniel