2

I have written a TYPO3 extension with a template. Inside that template I have a JavaScript function between <script> tags. It came to my attention just now that the whole template is rendered by Fluid (correct?). Hence it should do something (or rather should not, because it's javascript and shouldn't be considered by Fluid) with the curly brackets (function (){...}) of my JavaScript function inside my <script> tags. Correct? This doesn't seem to be the case since my code has always worked. I'm wondering why it has always worked? Does TYPO3 recognize the script tags and ignores everything inside of them? This is TYPO3 6.2 by the way.

Now the more important question: how can I access an associative array, which I have assigned to the view via the controller, inside the javascript function which is between my <script> tags? I tried something like var x = {test_variable} inside my javascript function but this doesn't work. That's a hint that curly brackets inside JavaScript are not interpreted by Fluid. But I found sources which claim that they are. However the curly brackets seem to get ignored by Fluid in my case. Which explains why my javascript has always worked. I'm confused because I cannot explain all these things. Anyway, so how do I access the variables, that I have assigned to my view in the controller, inside my JavaScript? I hope it's clear what I mean.

2 Answers 2

3

To have the answer in short. This seems to be a caching / rendering bug in TYPO3. A valid workaround is to surround the code causing problems with a condition.

So

...
var x = {test_variable}
...

can be written as

<f:if condition="1">
    ...
    var x = {test_variable}
    ...
</f:if>

Thanks to the research and comments of @Robert to the answer from Bernd Wilke
(This bug still existed in TYPO3 v9.5.23. Not tested in V10.4)

Sign up to request clarification or add additional context in comments.

6 Comments

yes, this bug is still present in v10.4.12
... aand still present in v10.4.14
... and according to @Erwin still in 10.4.15.
Mh I had code like this working inTYPO3 v8. After updateto TYPO3 v10 it was broken and fluid vars got not replaces anymore. I needed to wrap the var declaration with the <if> to make it work again. Thanks!
... error still present in TYPO3 11.5.30.
|
2

Fluid genereates a text. For Fluid there is no difference whether you generate XML, txt, HTML, Javascript or PHP.

In this way you can use Fluid variables everywhere. But you need to consider the special handling of curly brackets for variables in output which contains curly brackets as plain output. Fluid could mistreat a curly bracket which should be plain output as lead-in for a variable.
(There are similar problems if you generate multi-line javascript in typoscript)

Not every usage of curly brackets can be identified uniquely.

If the variable in your example var x = {test_variable} contains some text you missed the ' or " to indicate a string for javascript. But that should result in a javascript syntax error.
If you find {test_variable} literally in your javascript you might have not defined a Fluid variable named test_variable there. (inspect with <f:debug>{_all}</f:debug>)

5 Comments

Now this is funny: when I put your debug command before my <h1> {test_variable} </h1>, the variable is rendered, i.e. I see it's value on the website (which is "hello?"). But if I remove the debug command, all I see is an h1 headline with the literal text as it's value ("{test_variable}" instead of "hello?"). I do not understand why? I have assigned that variable in the controller via $this->view->assign('test_variable', 'hello?');
oh sorry, I forgot to mention: I switched from checking the presence of that variable in the javascript part to checking whether it's even accessible in the HTML part. And I assign and try to access that variable the way I do because that is how it is described here: docs.typo3.org/typo3cms/ExtbaseFluidBook/8-Fluid/… (they do it with the "blogTitle" variable)
I found the reason but I do not understand it: I can use <h1> {test_variable} </h1> AS SOON AS I have had code with <f: somewhere in the HTML document (<f:for or <f:debug or whatever). After <f: ... I can use <h1> {test_variable} </h1> and it's being rendered. Why is that?
that sounds like a (fluid caching?) bug. but as your TYPO3 version is out of service you probably will get no fix. You should check whether is also occurs in 8LTS and 9LTS.
We're still running into the same issue in the latest TYPO3 v9 LTS (9.5.23). Is there an open issue for this problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.