简体   繁体   English

TYPO3 将 Fluid 变量值传递给 Javascript

[英]TYPO3 pass Fluid variable value to the Javascript

In a fluid template I have a fluid variable which value I would like to use in my JavaScript code.在流体模板中,我有一个流体变量,我想在我的 JavaScript 代码中使用该值。

I am using JavaScript inside of fluid template.我在流体模板中使用 JavaScript。

My Code:我的代码:

<!-- value I would use further in my javascript -->
<h1 id="product-model">{product.model}</h1>

<!-- Javascript code (in the same file) -->
<script>
   <![CDATA[
     function printProductWindow() {
       document.title = document.getElementById("product");
       window.print(); 
     } 
   ]]>
</script>

Thanks in advance!提前致谢! Denis丹尼斯

Your element id is wrong: 您的元素ID错误:

 document.title = document.getElementById("product-model");

because you have defined it as id="product-model" . 因为您已将其定义为id="product-model"

Alternative, if your JavaScript is in your FluidTemplate, you can also set the Value there: 或者,如果您的JavaScript位于FluidTemplate中,则还可以在此处设置Value:

<script>
  <![CDATA[
    function printProductWindow() {
      document.title = "]]>{product.model}<![CDATA[";
      window.print(); 
    } 
  ]]>
</script>

But let me warn you: changing the title via JavaScript is not a good practice. 但是让我警告您:通过JavaScript更改标题不是一个好习惯。

Have an look at the TitleTagViewHelper from the news extension here to see one solution how this can be solved. 这里的新闻扩展中查看 TitleTagViewHelper,以了解一种解决方案。

<script>
        function printProductWindow() {
            document.title = <f:format.raw>{product.model}<f:format.raw>;
            window.print(); 
        }
</script>

For TYPO3 8.7. 对于TYPO3 8.7。

you can use the fluid variables in js as well but most of the time you need to use a format viewhelper like <f:format.htmlspecialchars> . 您也可以在js中使用Fluid变量,但是大多数时候您都需要使用<f:format.htmlspecialchars> format.htmlspecialchars <f:format.htmlspecialchars>类的格式viewhelper。

<script>
    <![CDATA[
        function printProductWindow() {
            document.title = '<f:format.htmlspecialchars>{product.model}</f:format.htmlspecialchars>';
            window.print(); 
        }
    ]]>
</script>

https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ThingsToKnow/JsAndInline.html https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ThingsToKnow/JsAndInline.html

<f:format.cdata>
   <script type="text/javascript">
      var myLayout;
      $(document).ready(function() {
         myLayout = $('body').layout({
            north__size: 27,
            north__initClosed: false,
            north__initHidden: false,
            center__maskContents: true // IMPORTANT - enable iframe masking
         });
      });
   </script>
</f:format.cdata>

Another way would be:另一种方法是:

<!-- value I would use further in my javascript -->
<h1>{product.model}</h1>

<!-- Javascript code (in the same file) -->
<script>
     function printProductWindow() {
       <f:format.raw>
       document.title = {product.model};
       window.print(); 
       </f:format.raw>
     } 
</script>

Put the Viewhelper <f:format.raw> to the closest curly brackets which shoul output a variable to get the fluid detection right.将 Viewhelper <f:format.raw> 放在最接近的大括号中,该大括号应为 output 变量,以便正确检测流体。

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

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