【问题标题】:Combining TypoScript and Fluid: Iterations?结合 TypoScript 和 Fluid:迭代?
【发布时间】:2016-03-29 14:13:52
【问题描述】:

我正在将 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 为每个内容元素应用的,所以我无法访问流体的 for-each 迭代信息。所以,我不能这样做:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以制作自己的 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}.

    【讨论】:

    • 我猜你不只是临时写的——你也有那个用例吗?你认为这是一个有效的场景吗?
    • 当然是。我将在我的一个项目中使用那个 DataProcessor。
    • ehem... 我把它放到EXT:template/Classes/DataProcessing/IteratorProcessor.php 中,命名空间为STUBR\Template\DataProcessing 并用renderObj.dataProcessing.20 = STUBR\Template\DataProcessing\IteratorProcessor 调用它——这给了我一个例外Processor class name "STUBR\Template\DataProcessing\IteratorProcessor" does not exist。我必须在某处注册 DataProcessor 吗?
    • 无需注册。您只需要确保您的类已加载。 :) Bennis 的回答也很合法。只要您不需要在迭代器中添加流体自己的迭代无法做到的特殊内容,我就会采用他的方法。
    【解决方案2】:

    您应该查看 TYPO3 核心附带的 DatabaseQueryProcessor。

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

    请注意,数据处理仅在 FLUIDTEMPLATE cObject 内工作。

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

    【讨论】:

    • 使用 DatabaseQueryProcessor 而不是 renderObj,迭代已经可用了,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多