Reputation: 49
How can I convert this instruction :
<script>
<?php
echo "sectionsPreview=".json_encode($this->groups).";\n";
echo "typographyFonts=".json_encode($this->fontsDropdown).";\n";
?>
</script>
to Twig?
I did this:
{{ ((sectionsPreview'~groups|json_encode|raw) }}
Upvotes: 1
Views: 209
Reputation: 304
You can make this if you want that your output text
{{ 'sectionsPreview='~groups|json_encode() }}
Upvotes: 0
Reputation: 64476
In twig you can set js variables as
<script>
var sectionsPreview = JSON.parse('{{ groups|json_encode() }}');
var typographyFonts= JSON.parse('{{ fontsDropdown|json_encode() }}');
</script>
you may need JSON.parse
to parse your string as json object
Upvotes: 2