TYPO3: pass variable to typoscript via cObject?

2019-06-03 19:52发布

问题:

I would like to create a dropdown login form in my menu, like in this example: http://bootsnipp.com/snippets/featured/fancy-navbar-login-sign-in-form

I have this cObject that calls typoscript for the navigation:

<f:cObject typoscriptObjectPath="menu.navbar" />

I need to get the content of the login form somehow into the menu typoscript. Is it maybe possible to pass a variable (in my case the login form) to typoscript via cObject ?

回答1:

f:cObject has a data Attribute, that can take different kind of values.

Usually the data attribute takes an array and you then can use those values to render content objects using the .field properties in typoscript.

An example:

lib.testFluid = COA
lib.testFluid {
    wrap = <div>|</div>
    10 = TEXT
    10.field = title
    10.wrap = <b>|</b>
    20 = TEXT
    20.field = content
}

If you have TypoScript like that, a data array, that has the keys title and content is expected. Rendering such a content object would possibly look like this in fluid:

<f:cObject typoscriptObjectPath="lib.testFluid" data="{title: 'Hello World', content: 'Foobar'}" />

However, if you just have some "content" (e.g. string content) and want to output it at one place in your content object, you can pass it in as-is and use the .current property in TypoScript to let it use the "current value".

lib.testFluid = COA
lib.testFluid {
    wrap = <div>|</div>
    10 = TEXT
    10.current = 1
    10.wrap = <b>|</b>
}

And in fluid:

<f:cObject typoscriptObjectPath="lib.testFluid" data="simple text content" />

or

<f:cObject typoscriptObjectPath="lib.testFluid">simple text content</f:cObject>

Of course data also takes normal variables. Depending on your use case, one of those cases might be what you want.

Edit: However, it seems to be a bit more complicated, if you want to use data together with an HMENU. The nested TMENU instances (or other menus) have different data values because it's being overwritten by HMENU with the current page for that menu entry. You probably have to do some convoluted wrapping, or avoid inserting the desired content in a TMENU/GMENU et cetera. I suggest to instead render the menu completely with fluid in that case.

Edit 2 - Example

Something like this is not going to work:

lib.testFluid = HMENU
lib.testFluid {
    special = directory
    special.value = 1
    wrap = <ul>|</ul>

    1 = TMENU
    1 {
        NO.stdWrap.cObject = COA
        NO.stdWrap.cObject {
            10 = TEXT
            10.field = title
            10.noTrimWrap = || |

            20 = TEXT
            20.current = 1
        }
    }
}

20.current = 1 won't include the value from data supplied by the fluid viewhelper, because the "data" of TMENU has been changed to the current page by the HMENU content object.

However, it should be possible to wrap a COA or similar around the HMENU to insert the desired content somewhere around the HMENU.