Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig - JavaScript booleans

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.

like image 294
Carlton Avatar asked Oct 20 '14 13:10

Carlton


2 Answers

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

like image 66
Niels Keurentjes Avatar answered Sep 30 '22 03:09

Niels Keurentjes


You can use json:

<script>
  var myBool = {{ mySuppliedValue | json_encode }};
</script>
like image 34
Docal Avatar answered Sep 30 '22 02:09

Docal