首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结合TypoScript和Fluid:迭代?

结合TypoScript和Fluid:迭代?
EN

Stack Overflow用户
提问于 2016-03-29 22:13:53
回答 2查看 826关注 0票数 1

我正在将一个TypoScript内容对象与一个fluid模板结合起来。

在页面模板中:

代码语言:javascript
复制
<f:cObject typoscriptObjectPath="lib.myItem" />

在TS中:

代码语言:javascript
复制
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中:

代码语言:javascript
复制
{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为每个content元素应用的,所以我无法访问fluid的for-each迭代信息。所以,我不能这样做:

代码语言:javascript
复制
  <f:for each="{data}" as="item" iteration="itemIterator">
  {itemIterator.cycle}
  </f:for>

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

如何获取renderObj产品的迭代信息?只有在TS中才有像http://typo3-beispiel.net/index.php?id=9中那样的老而可怕的计数器?

EN

回答 2

Stack Overflow用户

发布于 2016-03-29 22:23:51

你可以制作你自己的IteratorDataProcessor:

代码语言:javascript
复制
<?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中,您通过处理器传递数据:

代码语言:javascript
复制
dataProcessing {
    10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
    10 {
        references.fieldName = image
    }
    20 = Vendor\MyExt\DataProcessing\IteratorProcessor
}

在fluid中,您可以使用例如{iterator.isFirst}访问您在DataProcessor中设置的内容。

票数 2
EN

Stack Overflow用户

发布于 2016-03-30 19:13:22

你应该看看TYPO3内核附带的DatabaseQueryProcessor。

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

请注意,数据处理只在FLUIDTEMPLATE cObject中工作。

您还可以在文档中找到一个工作示例。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36286905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档