Passing variables to the JavaScript frontend

Hi - does Pimcore have any preferred / standardized way to pass variables to the JavaScript frontend? Of course you can do it yourself by putting a tag with global variables into the template layouts or by using e.g. data attributes.

However I have seen way better approaches like e.g. the wp_localize_script() function in WordPress: https://developer.wordpress.org/reference/functions/wp_localize_script/

Does there exist something like this? Or maybe is it worth implementing it?

data-attributes is the way I go…

1 Like

Ok thanks. There might be better solutions to this but as there seems to be no standard solution I guess I will write some twig extension for this.

If you’re looking for something that you can quickly parse into JS, take a look at this solution.

<script id="data" type="application/json">{"org": 10, "items":["one","two"]}</script>
<script>var data = JSON.parse(document.getElementById('data').innerHTML);</script>

Source: