How to write a filter for json_encode?
- szisti
- Member | 4
I meant that I want to be able to use latte inside a script tag like this:
var jsonObject = {$object|json_encode}
The only way I can make this work, is by writing it like this: var jsonObject =
{$object|json_encode|noescape}
Here's a stackoverflow link: https://stackoverflow.com/…php-latte-v3
- Jan Tvrdík
- Nette guru | 2595
Because Latte understands HTML contexts and knows you are in JavaScript
context, it will apply json_encode
automatically, so it's enought
to write
<script>
var jsonObject = {$object}
</script>
See Context-Aware Escaping and Printing in JavaScript chapters in documentation.