Jump to content

How to pass a smarty variable to a JS


Recommended Posts

I'm trying to implement a countdown module which uses a JS to display remaining special price time.

1) In module.php file I read $special_price->to value and assign it to smarty array


'expiry' => $specific_price->to,


test 1: After reading a lot of topics in various forums and sites I tried to pass it as a global variable

In module.tpl file I want to pass 'expiry' value to JS class


<script type="text/javascript">
var priceExpiryDate = "{$expiry}";

In js class I use it to initialize counter


var ts = new Date(priceExpiryDate);
left = Math.floor((ts - (new Date())) / 1000);

This solution works fine with Chrome but it doesn't work with Firefox and IE.


I tried to set global variable using alert("{$expiry}") and it doesn't work - it just opens a pop-up message in IE.


test 2:

In module.tpl file I set 'expiry value to a hiddent <span>

<span id="expiry_date" style="display:none">{$expiry}</span>

In js class I try to read it using

var ts = $('#expiry_date').text() or var ts = $('#expiry_date').val()

In both text() and val() cases date is not retreived from html.


I tried <span class="expiry_date" style="display:none">{$expiry}</span> and var ts = $('span.expiry_date').text() and it doesn't work, too.


What am I doing wrong?

Link to comment
Share on other sites


Thanks for your comment. I added a $(document).ready at the beginning of JS and I use solution 1.


It didn't solve my problem but I found the reason for incorrect calculation.

It appears that different SQL date formats are handled in IE and non-IE Date object. My date is "2013-04-30 00:00:00" and IE does not support this format.



Therefore I added a small piece of code to convert SQL date to Date object:


var tstr = priceExpiryDate;
//Convert SQL date from string to JS Date object
tstr = tstr.replace(/:| /g,"-");
 var YMDhms = tstr.split("-");
 var sqlDate = new Date();
 sqlDate.setFullYear(parseInt(YMDhms[0]), parseInt(YMDhms[1])-1,
 sqlDate.setHours(parseInt(YMDhms[3]), parseInt(YMDhms[4]), 
                                             parseInt(YMDhms[5]), 0/*msValue*/);

left = Math.floor((sqlDate - (new Date())) / 1000);


Now it works fine with all browsers, except for CSS in IE 9.0 which is a new story :)

Link to comment
Share on other sites

  • 2 years later...
  • 3 months later...

I've read at smarty and PS sites, that it's not "good practice" to put the JS code into TPL file.




But I don't know other solution, so I had to use the same solution as you described.


Maybe some one could give us proper solution how to pass variable from PHP to JS ?


Did you solve it ? I'm doing a module and I'm stuck on that part :/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...