Inside my custom views template, views-view-myview.tpl.php, I have a variable ("$myvar"), which I need to pass to a custom javascript function. Here is how I am currently doing it:
A. json-encode $myvar
<?php
$my_json_var = json_encode($myvar);
?>
B. Pass the variable as an argument to my custom function, on page load
<script>
Drupal.behaviors.mystuff = function (context) {
my_js_function(<?php print $my_json_var; ?>);
});
</script>
I am noticing that Drupal developers generally pass variables to javascript using Drupal.settings, and I am wondering whether I should switch and use Drupal.settings instead.
Question: Is there any disadvantage of doing things my way, as described above? Is there any reason it would be better to switch to Drupal.settings. Note: $myvar is relatively large, say 100k.
Thanks very much