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.
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.
Share Improve this question asked Oct 20, 2014 at 13:02 CarltonCarlton 5,7314 gold badges55 silver badges77 bronze badges2 Answers
Reset to default 9<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>