Twig - JavaScript布尔值

Car*_*ton 5 javascript php types twig

是否有一个twig函数,允许我将包含PHP布尔值的变量转换为文字JavaScript布尔值?

目前,我的PHP中的"true"值在我的twig模板中转换为"1".我已经尝试了一些转义函数,但到目前为止还没有任何工作.

Nie*_*jes 7

<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)

查看文档.

  • 这有点打破了如何从Twig的最终整合它的多个例子...... (4认同)

Doc*_*cal 5

您可以使用 json:

<script>
  var myBool = {{ mySuppliedValue | json_encode }};
</script>
Run Code Online (Sandbox Code Playgroud)