I have the jssor slider installed and working. I am trying to add the ability to change the slide type from an external file using php. In my unedited jssor code, if I enter the following as the slide types, they work as expected:
{$Duration:800,$Opacity:2},
{$Duration:1000,y:1,$Easing:$Jease$.$InBounce,$Opacity:2}
When I try to load from a function, as illustrated in the code below, the output of the console command is
0: "{$Duration:800,$Opacity:2},"
1: "{$Duration:1000,y:1,$Easing:$Jease$.$InBounce,$Opacity:2}"
If I strip off the quotes and place those in the _SlideshowTransitions function they work. So is it the quotes that are causing a problem? If so, how can I remove those? Or is it that the jssor function won't work with a variable? Or something else?
function GetTransitions() {
$xsisitons = array();
$xsisitons[] = '{$Duration:800,$Opacity:2},';
$xsisitons[] = '{$Duration:1000,y:1,$Easing:$Jease$.$InBounce,$Opacity:2}';
return json_encode($xsisitons);
}
$jsondata = GetTransitions();
<script>
var xsitions = <?php echo $jsondata; ?>;
console.dir(xsitions);
var _SlideshowTransitions = [
xsitions
];
</script>
Simply create the JS directly:
<?php
function GetTransitions() {
return $rawJS = '[
{
$Duration: 800,
$Opacity: 2
},
{
$Duration: 1000,
y: 1,
$Easing: $Jease$.$InBounce,
$Opacity: 2
}
]';
}
$options = GetTransitions();
?>
<script>
var xsitions = <?php echo $options; ?>;
console.dir(xsitions);
var _SlideshowTransitions = xsitions;
</script>
This is the line that prevents you from using json_encode
:
$Easing: $Jease$.$InBounce,
json_encode
would treat $Jease$.$InBounce
as a string rather than JS code, preventing it from functioning as expected.
That makes GetTransitions
extremely dangerous, which is hinted at by the new variable name rawJS
. Make sure you pay attention to what that function outputs.
You could safely make $Duration
, $Opacity
, and y
dynamic in the PHP by casting input variables to int when building the $rawJS
string, or by escaping the dynamic values (but not the whole string) with json_encode
:
function GetTransitions($duration_1, $duration_2, $opacity, $y) {
return $rawJS = '[
{
$Duration: ' . (int)$duration_1 . ',
$Opacity: ' . (int)$opacity . '
},
{
$Duration: ' . (int)$duration_2 . ',
y: ' . (int)$y . ',
$Easing: $Jease$.$InBounce,
$Opacity: ' . (int)$opacity . '
}
]';
}