TYPO3 - how to properly define constant, store it into variable and use inside of fluid template
Asked Answered
F

4

7

In the Fluid template of a plugin I am working on, some things are hardcoded. For instance:

<f:page.link pageUid="114">link</f:page.link>

Since pageUid values are not the same on the test and production servers I would like to make this more dynamic.

I would like to store the pageUid in a variable and then use the variable in the Fluid template.

Flan answered 31/1, 2017 at 10:58 Comment(0)
F
8

Because it is an setting do it like this:

Constants:

plugin.myext.settings.detailPid = 123

Setup:

plugin.myext.settings.detailPid = {$plugin.myext.settings.detailPid}

Variables are for variable content. If you have the same PID using variables with TEXT or similiar is overdressed and settings are the correct way.

Also variables are only accessable for FLUIDTEMPLATE content element, not for plugins!

Also in your extbase controller you can access these settings by simple access $this->settings['detailPid']without to render the cObjects first.

In your fluid you can access settings by {settings.detailPid}.

Finochio answered 31/1, 2017 at 14:30 Comment(3)
I don't understand this sentence "If you have the same PID using variables with TEXT or similiar is overdressed and settings are the correct way."Poll
@SybillePeters this sentence was related to another answer which used variables and pageUid = TEXT.Sacrilegious
I find this answer a bit difficult to understand. You use the term "Variables". Do you mean variables in FLUIDTEMPLATE? Perhaps it might be helpful to update the answer so it can be clearer. I think it's just those 2 sentences, the rest ist clear.Poll
E
5

In typoscript template for your content object FLUIDTEMPLATE:

Typoscript setup/configuration:

10 = FLUIDTEMPLATE
10 {
    variables {
        pageUid = TEXT
        pageUid.value = 114
    }
}

or using constants

Typoscript constants:

pageUid = 114

Typoscript setup/configuration:

10 = FLUIDTEMPLATE
10 {
    variables {
        pageUid = TEXT
        pageUid.value = {$pageUid}
    }
}

Then you can fetch pageUid in your Fluid HTML

<f:else>
    <li>
        <v:page.link pageUid="{pageUid}" />
    </li>
</f:else>

To use variables in a Fluid partial, make sure to pass these along, e.g. by providing _all:

<f:render partial="fluid_part_header" arguments="{_all}" />
Eisenhart answered 31/1, 2017 at 11:17 Comment(0)
S
0

Please consider using jokumer's solution. randomresult's solution would work, but I wouldn't suggest it. You don't need vhs to pass variables. Not talking about Paul Beck`s solution, because it wouldn't work at all. Not anymore at least. (See TYPO3\CMS\Fluid\ViewHelpers\CObjectViewHelper, https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/CObject.html). It accepts only content objects.

Set your Pid in constants like PID_SOME_PAGE = 123 and set a variable in your plugins settings. Like this:

plugin.tx_yourplugin {
  ...
  settings{
    somePage = {$PID_SOME_PAGE}
  }
  ...
}

You can bypass that constants version if you want and set your page id in settings directly. It's just a cleaner way in my opinion, especially for larger websites. Then you can use that variable in your template, like this <f:link.page pageUid="{settings.somePage}">Go to Page</f:link.page>. More options for f:link.page here: https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Link/Page.html

Schaeffer answered 31/1, 2018 at 15:51 Comment(0)
A
-1

If you use EXT:vhs anyways, you can do the following to:

TS-Constants:

pageUid=114

TS-Setup:

settings.pageUid = {$pageUid}

Template (Fluid)

<f:else>
    <li>
        <v:page.link pageUid="{v:variable.typoscript(path: 'settings.pageUid')}" />
    </li>
</f:else>

This will make it available for all FLUID Templates.

Astilbe answered 31/1, 2017 at 12:34 Comment(1)
You don't need vhs to access Typoscript. {f:cObject(typoscriptObjectPath: 'settings.pageUid')}} should do the job, too. <v:page.link pageUid="{pageUid}" /> can also be replaced by <f:link.page pageUid="{f:cObject(typoscriptObjectPath: 'settings.pageUid')}}" />Dropsical

© 2022 - 2024 — McMap. All rights reserved.