I found this way to send variables from PHP to Javascript.
Inside the template.tpl.php file, I have the following code.
<?php
echo '<div class="drupal-vars" style="display:none;">'.json_encode($variables).'</div>';
?>
Within Javascript, I have the following code:
(function ($) {
$(document).ready(function() {
var variables = jQuery.parseJSON($('.drupal-vars').html());
//Remove if youwant to clean the code.
$('.drupal-vars').remove();
});
}(jQuery));
I found another solution, but it's more complicated, although it may be better architectured.
- Is there a better way to do it?
- How ugly is what I am doing here?