简体   繁体   English

Typo3 Fluid-如何检查两个或多个colPos是否包含内容,然后打印HTML

[英]Typo3 Fluid - How do I check if two or more colPos have content and then print HTML

I want to print the following HTML only if one or more of the colPos has content in it. 我只想在一个或多个colPos中包含内容的情况下打印以下HTML。 If none have content elements in it, then I don't want to print this block (the whole "row") of HTML. 如果没有内容元素,那么我不想打印此HTML代码段(整个“行”)。

<div class="row">
  <div class="col-sm-4">
      <f:cObject typoscriptObjectPath="lib.dynamicContent" data="5" />
  </div>
  <div class="col-sm-4">
      <f:cObject typoscriptObjectPath="lib.dynamicContent" data="6" />
  </div>
  <div class="col-sm-4">
      <f:cObject typoscriptObjectPath="lib.dynamicContent" data="7" />
  </div>
</div>

I thought about getting the colPos and try to do a OR condition on Fluid. 我考虑过要获得colPos并尝试对Fluid进行OR条件。 But I have no idea on how to do it. 但是我不知道该怎么做。 I know I can check one by one like this: 我知道我可以像这样一一检查:

<f:if condition="{f:cObject(typoscriptObjectPath: 'lib.dynamicContent', data: '5')}">
   ...HTML for colPos 5 HERE...
</f:if>

But I don't want to do that. 但是我不想那样做。 In my template I have almost 50 different colPos and they are organized by blocks (rows). 在我的模板中,我几乎有50种不同的colPos,它们是按块(行)组织的。 Like colPos 1 to 5 is one block(row). 像colPos 1到5一样是一个块(行)。 colPos 10 to 25 in another block(row). 在另一个块(行)中将位置10到25。 But some pages will not use some blocks (rows) of colPos, so there's no reason on printing the HTML code for those blocks (rows) of colPos unused. 但是某些页面不会使用colPos的某些块(行),因此没有理由为未使用的colPos的这些块(行)打印HTML代码。

Thanks for your help! 谢谢你的帮助!

A fluid-only solution would be to assign the results of the <f:cObject> -ViewHelpers each to a variable, and then use the concatenation of these variables in a condition. 唯一可行的解​​决方案是将每个<f:cObject> cObject <f:cObject> -ViewHelpers的结果分配给一个变量,然后在条件中使用这些变量的串联。 The v:-namespace in the example is the namespace of the extension vhs: 示例中的v:-namespace是扩展名vhs的名称空间:

<v:variable.set name="col-5" value="{f:cObject(typoscriptObjectPath: 'lib.dynamicContent', data: '5')}"/>
<v:variable.set name="col-6" value="{f:cObject(typoscriptObjectPath: 'lib.dynamicContent', data: '6')}"/>
<v:variable.set name="col-7" value="{f:cObject(typoscriptObjectPath: 'lib.dynamicContent', data: '7')}"/>

<f:if condition="{col-5}{col-6}{col-7}">
    <div class="row">
        <div class="col-sm-4">{col-5}</div>
        <div class="col-sm-4">{col-6}</div>
        <div class="col-sm-4">{col-7}</div>
    </div>
</f:if>

You should of course move this stuff to a partial, which gets an array of the columns to print as a parameter. 您当然应该将这些内容移至部分内容,该内容将获取列数组以作为参数进行打印。 Then you need to write the logic only once. 然后,您只需编写一次逻辑。

Also, you should think again, if you really need 25 columns. 另外,如果您确实需要25列,则应重新考虑。

Since TYPO3 8.6, this is possible without extension "vhs": 从TYPO3 8.6开始,无需扩展名“ vhs”就可以实现:

<f:variable name="col-5">
    <f:cObject typoscriptObjectPath="lib.dynamicContent" data="5" />
</f:variable>
<f:if condition="{col-5}">
     <f:format.raw>{col-5}</f:format.raw>
</f:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM