结合TypoScript和Fluid:迭代?

时间:2016-03-29 14:13:53

标签: typo3 typoscript fluid typo3-7.x typo3-7.6.x

我正在将TypoScript CONTENT对象与流体模板相结合。

在页面模板中:

<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应用了模板,所以我无法访问有关迭代的每个信息的流体。所以,我不能这样做:

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

找出我们在哪个呈现的项目中...因为每个元素都由renderObj单独呈现。

如何获取有关renderObj产品的迭代信息?仅在TS中使用旧的和可怕的计数器,如http://typo3-beispiel.net/index.php?id=9

2 个答案:

答案 0 :(得分:2)

您可以创建自己的IteratorDataProcessor:

<?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;
    }
}

在Typoscript中,您通过该处理器传递数据:

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

在流体中,您可以访问您在DataProcessor中设置的内容,例如: {iterator.isFirst}

答案 1 :(得分:2)

您应该查看TYPO3 Core附带的DatabaseQueryProcessor。

https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Fluidtemplate/Index.html#dataprocessing

请注意,数据处理仅适用于FLUIDTEMPLATE cObject。

您还可以在文档中找到一个有用的示例。