php inside literal script - .tpl

谁说胖子不能爱 提交于 2019-12-13 05:45:58

问题


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}

回答1:


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}

Addendum:

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

{$obj|json_encode}


来源:https://stackoverflow.com/questions/13143561/php-inside-literal-script-tpl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!