文字脚本中的php-.tpl [英] php inside literal script - .tpl

查看:91
本文介绍了文字脚本中的php-.tpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文字javascript块内创建json_encode?基本上,这是一个聪明的模板.

How can i make a json_encode inside a literal javascript block? Basically this is smarty template.

 {literal}
    <script>
        function openWin() {
            var O = {php} echo json_encode($obj);{/php}; // syntax error         
            alert(O);
        }
    </script>
 {/literal}

推荐答案

已经有一段时间了,因为我不得不摆弄Smarty模板,但是我认为您必须关闭文字标签才能运行php代码.如果遇到错误,请尝试也删除echo-我不记得当它位于smarty标记中时是否为隐式的.

It's been a while since I had to fiddle with a Smarty template but I think you'd have to close the literal tag to run your php code. If you get an error, try removing the echo too - I can't remember if it's implicit or not when inside a smarty tag.

{literal}
  <script>
    function openWin() {
      var O = {/literal}{php}echo json_encode($obj);{/php}{literal};       
      alert(O);
    }
  </script>
{/literal}

附录:

用于回显变量的巧妙语法也可以容纳过滤器"(即函数)

Smarty syntax for echoing variables can also accommodate "filters" (ie. functions)

{$obj|json_encode}

这篇关于文字脚本中的php-.tpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆