I'm trying to find the best way to use javascript with Mustache and PHP.
I have for example a mustache template:
<div>My html {{myvariable}}</div>
<script id="uniqueID">
console.log('{{myvariable}}');
// do something
</script>
My mustache templates are retrieved from an API, for example when I click on a button on a page. The API sends back a JSON to the client with the rendered mustache html template inside the JSON.
When doing this, the javascript is of course not executed.
If I add the javascript to the and add the name of the script ID in an array to avoid adding the same script multiple times, it works. If this template needs to be executed multiple times on the page and the script is already in , the script will not be executed anymore.
It would work with eval() but I'm trying to avoid this as it goes against my CSP settings.
I need to have the JS included in the template because it needs to be populated with variables coming from PHP.
How could I solve this ?
Thanks