Car*_*ton 5 javascript php types twig
是否有一个twig函数,允许我将包含PHP布尔值的变量转换为文字JavaScript布尔值?
目前,我的PHP中的"true"值在我的twig模板中转换为"1".我已经尝试了一些转义函数,但到目前为止还没有任何工作.
<script>
// You can use it in literal code like this:
var myBool = {{ mySuppliedValue ? 'true' : 'false' }};
// Or in clientside string constants like this:
console.log('The value is {{ mySuppliedValue ? 'true' : 'false' }}');
</script>
Run Code Online (Sandbox Code Playgroud)
查看文档.
您可以使用 json:
<script>
var myBool = {{ mySuppliedValue | json_encode }};
</script>
Run Code Online (Sandbox Code Playgroud)