问题
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