【发布时间】:2014-12-15 11:27:49
【问题描述】:
是否有一个 twig 函数可以让我将包含 PHP 布尔值的变量转换为文字 JavaScript 布尔值?
目前,我的 PHP 值“true”在我的 twig 模板中转换为“1”。 我已经尝试了一些转义函数,但到目前为止没有任何效果。
【问题讨论】:
标签: javascript php types twig
是否有一个 twig 函数可以让我将包含 PHP 布尔值的变量转换为文字 JavaScript 布尔值?
目前,我的 PHP 值“true”在我的 twig 模板中转换为“1”。 我已经尝试了一些转义函数,但到目前为止没有任何效果。
【问题讨论】:
标签: javascript php types twig
<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>
【讨论】:
myBool,因为您已经检索到它。
{{ myBool }} - 那时该变量存在于 JS 中,而不是 Twig 中。
console.log('The value is ' + myBool);
你可以使用json:
<script>
var myBool = {{ mySuppliedValue | json_encode }};
</script>
【讨论】: