I'm using the flot module to create some graphs in Drupal. GenericallyGenerally, it is working pretty well. However, I have come across an issue which is more general.
I am defining a settings array, which works for most things, howeverbut some flot settings allow for anonymous functions:
xaxis: {
transform: function (v) { return Math.log(v); },
inverseTransform: function (v) { return Math.exp(v); }
}
howHow could I encode this in phpPHP so that it will be output correctly? theThe flot module uses drupal_to_js()
, and I can't put it outside of quotes as phpPHP will try to interpret it. If I put it inside quotes it will be treated as a string.
From my googleing it looks like json_encode()
also doesn't support this.
edit to try to explain more
Drupal_to_jsA little more explanation: drupal_to_js()
takes a phpPHP structure and transposes it to javascriptJavaScript.
<?php
$top = array(
'a_string' => 'A string will convert fine',
'a_number' => 123 // as will a number,
'an_array' => array('a' => 'nested arrays work fine too'),
);
This will be transposed to something like
[ a_string: 'A string will convert fine', a_number: 123 ...
I want to transpose a functionfunction; the following code will give me the string 'function (v) { return Math.log(v); }'
.
<?php
$top = array(
'a_string' => 'function (v) { return Math.log(v); }',
);
WillThe following code will give me the string 'functiona PHP parse error (vas it should be) { return Math.log(v); }'
<?php
$top = array(
'a_string' => function (v) { return Math.log(v); },
);
Will give a php parse error (and so it should).
I want the text in the string to be used as a JSJavaScript literal. But, but I don't think it can be done.
Has anyone found a good way round this? I could output some raw js.JavaScript, but then I will looselose a lot of functionality.