Twig - JavaScript booleans

Is there a twig function that will allow me to convert a variable containing a PHP boolean to a JavaScript boolean literal?

Currently my "true" value from PHP is being converted to "1" in my branch template. I've tried several exit functions but nothing seems to work so far.

+3


source to share


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

+6


source


You can use json:



<script>
  var myBool = {{ mySuppliedValue | json_encode }};
</script>

      

+1


source







All Articles