Page 1 of 1

problem to add file path to javascript variable

Posted: Fri Aug 23, 2019 4:02 pm
by Sendlingur
I've been spending most of my day trying to figure out how to pass a filepath to a javascript variable in an external .js file. I have done this before in a plain .php / .js files, but never in CMSMS.

What I need is to get the path from this file

Code: Select all

{$entry->file_location}/{$fields.Audio1->displayvalue}{$entry->fields.Audio1->displayvalue}
and assign the path to a javascript variable which is in this location

Code: Select all

/uploads/js/id3.js
I've tried every php variable to js variable method I've found on stackoverflow, google, etc to do this but with no luck.

I've wrapped plain php and javascript in {literal} but nothing is working out.

I have also tried to use the smarty {capture} and {assign} but still it is not working.

Now I'm totally out of options...can anyone more experienced in cmsms help me out?

for more clarification: the file is an .mp3 and I need to assign it's path to the .js variable for reading the ID3 tags bound to the file.

Re: problem to add file path to javascript variable

Posted: Fri Aug 23, 2019 7:03 pm
by velden
You can not assign a smarty variable to be used outside the CMSMS scope/context.

You could however assign the value to a smarty variable and use that variable in your page template. So you can assign it to a global javascript variable which later can be used inside your /uploads/js/id3.js file

Code: Select all

<__script__>
var myPath = '{$entry->file_location}/{$entry->fields.Audio1->displayvalue}';
</__script>
<__script__ src="{uploads_url}/js/id3.js"></__script>
Inside the id3.js file you should use the 'myPath' variable.