简体   繁体   English

TYPO3:在 JavaScript 中使用 Fluid 变量?

[英]TYPO3: use Fluid variable in JavaScript?

I have written a TYPO3 extension with a template.我用模板编写了一个 TYPO3 扩展。 Inside that template I have a JavaScript function between <script> tags.在该模板中,我在<script>标签之间有一个 JavaScript 函数。 It came to my attention just now that the whole template is rendered by Fluid (correct?).刚刚我注意到整个模板都是由 Fluid 渲染的(对吗?)。 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.因此,它应该在我的<script>标签中使用我的 JavaScript 函数的大括号( function (){...} )做一些事情(或者更确切地说不应该做,因为它是 javascript 并且不应该被 Fluid 考虑)。 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? TYPO3 是否识别脚本标签并忽略其中的所有内容? This is TYPO3 6.2 by the way.顺便说一下,这是TYPO3 6.2。

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?现在更重要的问题是:如何在<script>标签之间的 javascript 函数中访问我通过控制器分配给视图的关联数组? I tried something like var x = {test_variable} inside my javascript function but this doesn't work.我在我的 javascript 函数中尝试了类似var x = {test_variable}东西,但这不起作用。 That's a hint that curly brackets inside JavaScript are not interpreted by Fluid.这暗示了 JavaScript 中的大括号不会被 Fluid 解释。 But I found sources which claim that they are.但我找到了声称它们是的消息来源。 However the curly brackets seem to get ignored by Fluid in my case.然而,在我的例子中,Fluid 似乎忽略了大括号。 Which explains why my javascript has always worked.这解释了为什么我的 javascript 一直有效。 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?无论如何,那么我如何访问在控制器中分配给我的视图的变量,在我的 JavaScript 中? I hope it's clear what I mean.我希望我的意思很清楚。

To have the answer in short.简而言之就是答案。 This seems to be a caching / rendering bug in TYPO3.这似乎是 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感谢@Robert 对Bernd Wilke回答的研究和评论
(This bug still existed in TYPO3 v9.5.23. Not tested in V10.4) (此bug在TYPO3 v9.5.23中依然存在,V10.4中未测试)

Fluid genereates a text.流体生成文本。 For Fluid there is no difference whether you generate XML, txt, HTML, Javascript or PHP.对于 Fluid,无论您生成 XML、txt、HTML、Javascript 还是 PHP,都没有区别。

In this way you can use Fluid variables everywhere.通过这种方式,您可以在任何地方使用 Fluid 变量。 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. Fluid 可能会误用大括号,该大括号应该是作为变量导入的普通输出。
(There are similar problems if you generate multi-line javascript in typoscript) (如果在排版中生成多行 javascript 也会出现类似问题)

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.如果示例中的变量var x = {test_variable}包含一些文本,则您错过了'"以指示 javascript 字符串。但这应该会导致 javascript 语法错误。
If you find {test_variable} literally in your javascript you might have not defined a Fluid variable named test_variable there.如果您在 javascript 中发现{test_variable}字面意思,您可能没有在那里定义名为test_variable的 Fluid 变量。 (inspect with <f:debug>{_all}</f:debug> ) (使用<f:debug>{_all}</f:debug>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM