最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Twig - JavaScript booleans - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

2 Answers 2

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>
发布评论

评论列表(0)

  1. 暂无评论