문제

I am using the extension fluidpages and want to switch the layout by the typeNum. is it possible to change the f:layout by by an condition? This wont work:

<f:layout name="{f:if(condition: '{typeNum} == 666', then: 'PageAjax', else: 'Page')}"/>
도움이 되었습니까?

해결책

Suggested approach:

<f:layout name="Page/{typeNum}"/>

Required files:

  • Resources/Private/Layouts/Page/0.html
  • Resources/Private/Layouts/Page/666.html

Please note: this only works if the {typeNum} variable is guaranteed to exist - if it does not, you will face a "template file not found" error with an empty filename. To avoid this, you can use the VHS extension's v:var.convert ViewHelper to ensure a proper value:

<f:layout name="Page/{typeNum -> v:var.convert(type: 'integer')}"/>

다른 팁

I have now added the Condition in the Layout template. I get the typeNum from typoscript.

<f:if condition="{f:cObject(typoscriptObjectPath:'plugin.nc_template.settings.pageLayout')} == 'Default'">
<p>Default Template</p>
</f:if>

<f:if condition="{f:cObject(typoscriptObjectPath:'plugin.nc_template.settings.pageLayout')} == 'Ajax'">
    <p>Ajax Template</p>
</f:if>

I found an example on the fedext Page, but could not get this to run: https://fedext.net/blog/archive.html?tx_news_pi1[news]=55&tx_news_pi1[%40widget_0][currentPage]=8&cHash=f9c3165598a28d2aa98fd30ef115bb75

I got the same problem recently, and found the solution after a few researchings.

The problem is, you can not use nested fluid like <f:if>, <f:cObject> or others in <f:layout>. Otherwise, you will get a fatal error in the cache file, saying call to a member function getViewHelper() on a non-object. And when you look at the cache file, you will find it's because $self is not defined.

Therefore, my solution is, searching public function getLayoutName( in \TYPO3\CMS\Fluid\Core\Compiler\TemplateCompiler, and adding \$self = \$this; before \$currentVariableContainer = \$renderingContext->getTemplateVariableContainer();, just like generateCodeForSection()

Can´t you use an if-statement instead? IMHO this is easier to read - and if you need to add more arguments which depends on typeNum, it would stay readable.

<f:if condition="{typeNum} == 666">
  <f:then>
    <f:layout name="PageAjax">
  </f:then>
  <f:else>
    <f:layout name="Page">
  </f:else>
</f:if>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top