Prestashop,在.tpl中,如何在<脚本内容

Prestashop, in .tpl, how to use smarty variable in <script content?

本文关键字:脚本 lt tpl Prestashop      更新时间:2023-09-26

我希望在javascript中使用一个智能变量,但我并非无法做到这一点><'

我的当前代码,但生成错误=>

<script type="text/javascript">
var title = document.title;
var height1 = {$mysmartyvariable1};
var height2 = {$mysmartyvariable2};
if (title == 'index')
{ var height = height1; }
else { var height = height2; };
$(document).on("scroll",function(){
    if($(document).scrollTop()>height){
        $("#nav").removeClass("navfull").addClass("navsmall");
    } else{
        $("#nav").removeClass("navsmall").addClass("navfull");
    }
});
</script>

那么,我如何在这个脚本部分添加mymartyvariable1呢?

#

感谢Allan Nienhuis,正确的代码是:

<script type="text/javascript">
var title = document.title;
var height1 = {$mysmartyvariable1};
var height2 = {$mysmartyvariable2};
{literal}
if (title == 'index')
{ var height = height1; }
else { var height = height2; };
$(document).on("scroll",function(){
    if($(document).scrollTop()>height){
        $("#nav").removeClass("navfull").addClass("navsmall");
    } else{
        $("#nav").removeClass("navsmall").addClass("navfull");
    }
});
{/literal}
</script>

我看到您的代码有两个问题。

1) 您没有在$mymartyvariable2周围放{}个括号。这将为height2分配"undefined",因为smarty根本不会处理此变量,并且未定义名为$mymartyvariable2的javascript变量。

2) 在嵌入使用{}个字符的javascript时,您需要使用{literal}{/literial}标记,这样smarty就不会试图将javascript{}个字符解释为smarty语法。

http://www.smarty.net/docsv2/en/language.function.literal

在这种情况下,您可以在height2声明/赋值之后启动{literal}标记,因为前面的行应该由smarty解释,但后面的行不应该由smart解释。

var height2 = {$mysmartyvariable2};
{literal}
if (title == 'index') 
  { //code here 
  }
// other code
{/literal}
</script>