Tom
Tom

Reputation: 1607

Clean way to switch between typo3 fluid page and action link

Is there a more clean way to switch between a page or a action link in fluid based on a var?

Now i used the if then statement but this increases a lot of double code lines. See example:

<f:if condition="{var}">
    <f:then>
        <f:link.page pageUid="{PageId}">

            // a lot of code lines

        </f:link.page>
    </f:then>
    <f:else>        
        <f:link.action pluginName="Name" controller="Cont">

            // the same a lot of code lines again

        </f:link.action>
    </f:else>
</f:if>

Upvotes: 2

Views: 729

Answers (1)

helmbert
helmbert

Reputation: 37994

You can extract the code within the links into a partial.

For this, first create a partial template. Inside an Extbase extension, they are placed in Resources/Private/Partials by convention (you can change this by using the setPartialsRootPath() method on the template object, but usually that shouldn't be necessary).

# Resources/Private/Partials/LinkContent.html
<!-- Insert your shared code lines here -->

Then reference the partial in your template:

<f:if condition="{var}">
    <f:then>
        <f:link.page pageUid="{PageId}">
            <f:render partial="LinkContent" />
        </f:link.page>
    </f:then>
    <f:else>        
        <f:link.action pluginName="Name" controller="Cont">
            <f:render partial="LinkContent" />
        </f:link.action>
    </f:else>
</f:if>

Note that you will have to pass variables explicitly into the partial from the parent template:

<!-- Assuming there's a variable "foo" in your template -->
<f:render partial="LinkContent" arguments="{foo: foo}" />

Alternatively, you can import the entire scope into the partial:

<f:render partial="LinkContent" arguments="{_all}" />

Upvotes: 1

Related Questions