Is there a twig function that will allow me to convert a variable that contains a PHP boolean value into a literal JavaScript boolean?
At the moment my value of "true" from PHP is converted to a '1' in my twig template. I've tried a few of the escape functions but nothing is working so far.
<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>
See the docs.
You can use json:
<script>
var myBool = {{ mySuppliedValue | json_encode }};
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With