NewbieSSProgrammer NewbieSSProgrammer -4 years ago 135
Javascript Question

Silverstripe include vars into JS or CSS files

I have found on Silverstripe website this custom js programmation :

Requirements::javascriptTemplate("cms/javascript/editor.template.js", $vars);

At the end of the requirements, how $vars must be inserted and is this possible to do the same action to css files?

Answer Source

Please look at the api docs for the information which shows...

if($vars) foreach($vars as $k => $v) {
    $search[] = '$' . $k;
    $replace[] = str_replace("\\'","'", Convert::raw2js($v));

$script = str_replace($search, $replace, $script);

So the following array...

$vars = array(
    'iVal'  => '99',
    'iVal'  => "'string'",

would transform the following JavaScript...

var iVal = $iVal;
var sVal = $sVal;


var iVal = 99;
var sVal = 'string';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download