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) }}
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
如果您希望输出文本,可以这样做
{{ 'sectionsPreview='~groups|json_encode() }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.