Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Passing a PHP-variable to a template.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
spk
Smarty n00b


Joined: 07 Jan 2014
Posts: 1

PostPosted: Tue Jan 07, 2014 12:57 pm    Post subject: Passing a PHP-variable to a template. Reply with quote

Hey,

I'm trying to create a timer that will show up in a div block inside my template.

The javascript works well and I can put any value to it and it will count it down by the number (milliseconds) it gets.
The problem for me is that I'm new to smarty and I can't pass the PHP-variable ($ms) into my template that my javascript will use.

The necessary parts of my template:
Code:

...
...
        <script>
            var millis = <?php echo json_encode($ms) ?>;

           //I want it like this: var millis = {$ms};

            function displaytimer(){
                var hours = Math.floor(millis / 36e5),
                    mins = Math.floor((millis % 36e5) / 6e4),
                    secs = Math.floor((millis % 6e4) / 1000);
            if (hours > 0){
                    document.getElementById('count').innerHTML = hours+':'+mins+":"+secs;
                }
            else {
                    document.getElementById('count').innerHTML = mins+":"+secs;
                    var nFilter = document.getElementById('count');
                    nFilter.setAttribute("style", "font-weight:bold;color: #E00;float:left;");
                }
            }

            setInterval(function(){
                millis -= 1000;
                displaytimer();
            }, 1000);
        </script>
...
...
...
...
<div id="leverans">
   <p style="float:left;">Tid tills n&auml;sta utskick&nbsp;</p>
   <p id="count" style="float:left;font-weight:bold;"></p>
</div>
...
...


My PHP-file which is in the same folder as the .tpl file:
Code:
<?php
$x = time();
$tre = mktime(14);
if ($x < $tre) {
   $y = strtotime('today 14:00:00');
}
else {
   $y = strtotime('tomorrow 14:00:00');
}
$result = floor(($y - $x) / 60);
$s = $result * 60;
$ms = $s * 1000;
?>


I've tried smarty assign and I tried to include the PHP-file in the .tpl.
Note that the script might not be optimized, but that doesn't matter for now. I just need to get it to work first. Embarassed
Appreciate every answer that can lead to something.

Edit: Everything works if i run everything within a php-file.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP