Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Convert this script to work with smarty
Goto page Previous  1, 2
 
Post new topic   Reply to topic    Smarty Forum Index -> Help Wanted (commercial)
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1105

PostPosted: Wed Oct 11, 2017 12:46 pm    Post subject: Reply with quote

Do you understand that when using
Code:
{assign $variable $value}
, you're assigining value to the variable named by the content of $variable?
Essentially, your code translates to PHP as
Code:
$$variable = $value;

Proof:
Code:
<?php

require_once __DIR__ . '/smarty-config.php';

$smarty->assign([
  'name' => "varname",
  'value' => "Value.",
]);

$smarty->display(__FILE__);

__halt_compiler()?>{assign $name "$value"}
name => {$name}
value => {$value}
varname => {$varname}
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Wed Oct 11, 2017 12:48 pm    Post subject: Reply with quote

No, I know nothing about Smarty coding! If someone can get this simple script to work (paid or unpaid) with Smarty I'd be grateful. Thanks
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1105

PostPosted: Wed Oct 11, 2017 2:08 pm    Post subject: Reply with quote

drgl wrote:
No, I know nothing about Smarty coding!

This is PHP basics.
Well, not strictly basics, but still.
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Wed Oct 11, 2017 2:19 pm    Post subject: Reply with quote

if you can help please either post how or your price to do it. I'm no coder, someone who has already been replying hasn't got it to work (I'm sure they know a LOT more than me!), the guy who wrote the original script doesn't know what it needs to work with Smarty so I have no hope!
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 118
Location: West Coast

PostPosted: Wed Oct 11, 2017 4:18 pm    Post subject: Reply with quote

The first block (of the three you last posted above) is where you changed the original PHP code. In an effort to remove some confusion, let's delete all the original PHP code. And I think you missed one of my instructions. So, use just the following:
Code:
          {nocache}{* Do not cache these variables. Always use fresh values. *}
          {$tz_was_set = date_default_timezone_set('GMT')}
          {$currentTime = $smarty.now|date_format:"%T"}
          {$currentDay = $smarty.now|date_format:"%w"}
          {$delTimeStart = '00:00:00'}
          {$delTimeEnd = '14:30:00'}
          {if $currentTime gte $delTimeStart && $currentTime lt $delTimeEnd && $currentDay gt 0 && $currentDay lt 6}
          {$css = 'display: block;'}
          {else}
          {$css = 'display: none;'}
          {/if}
         <div id="delcta" style="{$css}">
            <p>Time left for Free Next Day Delivery: <span id="countdowntimer"></span></p>
         </div>
         
         <script type="text/javascript">
          $('#countdowntimer')
            .countdown('{{$smarty.now + 86400}|date_format:"%Y/%m/%d"} 14:27:00')
            .on('update.countdown', function(event) {
             var $this = $(this).html(event.strftime(''
                + '%-H %!H:hour,hours; '
                + '%-M %!M:minute,minutes; '
                + '%-S %!S:second,seconds;'
             ));
          });
       </script>
       {/nocache}
</body>
</html>

You must view the resulting HTML source being shown in the browser. If nothing is shown, then the CSS for that div could be "display: none;" and we will need to find out why.
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Wed Oct 11, 2017 4:28 pm    Post subject: Reply with quote

Ok, to be clear. Add the above code snippet to the body template and the other code references (below) to the header?

Header :-

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="{$smarty.const.SITE_ROOT}/jquery.countdown.min.js"></script>


??
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Wed Oct 11, 2017 4:36 pm    Post subject: Reply with quote

Assuming that's what you meant I've done that. I had to change the times as nothing at all appeared (it's 5.30pm). All I get now is "Time left for Free Next Day Delivery: " with no count down timer. Viewing source in the browser shows this code is in the head correctly :-

Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://www.ourdomain.com/jquery.countdown.min.js"></script>


And this code appears in the body at the bottom :-

Code:
<div id="delcta" style="display: block;">
             <p>Time left for Free Next Day Delivery: <span id="countdowntimer"></span></p>
          </div>
           
          <script type="text/javascript">
           $('#countdowntimer')
             .countdown('2017/10/12 18:27:00')
             .on('update.countdown', function(event) {
              var $this = $(this).html(event.strftime(''
                 + '%-H %!H:hour,hours; '
                 + '%-M %!M:minute,minutes; '
                 + '%-S %!S:second,seconds;'
              ));
           });
        </script>
         
</body>
</html>
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 118
Location: West Coast

PostPosted: Wed Oct 11, 2017 6:00 pm    Post subject: Reply with quote

There is "display: block;", so we know that the Smarty code is working - as the test does actually evaluate to true.

That leaves determining if the Countdown script has been coded correctly.

Troubleshooting the javascript will be helped by learning how to use your browser's Developer Tools - specifically the javascript error console.

I will argue that this line:
Code:
var $this = $(this).html(event.strftime(''
is illegal as $this is a special variable that belongs exclusively to javascript and can only be read from, never anything assigned to it.

Other than that, I see no other reason why the script would fail to run (other than having installed the NoScript plugin for Firefox).

If the script does crash, it will be reported in the browser's javascript console.
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Wed Oct 11, 2017 6:04 pm    Post subject: Reply with quote

I don't think it's the JS script as the script works OK on a plain old PHP page. Assuming you mean the jquery.countdown.min.js?
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 118
Location: West Coast

PostPosted: Wed Oct 11, 2017 6:40 pm    Post subject: Reply with quote

I mean the script near the closing body and html tags at the bottom.

I have no answer why there is an actual display of the countdown effect when running the PHP version - but no effect when running the Smarty version.

BOTH VERSIONS send the same javascript to the browser and it is the browser's job to run the javascript -- not PHP and not Smarty.

So, please learn how to open and read your browser's javascript console.
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Thu Oct 12, 2017 8:23 am    Post subject: Reply with quote

Following instructions found online for Chrome, all I see is this on my test page that works :-

PHP

Code:
<html>

   <head>
   
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

      <script src="jquery.countdown.min.js"></script>
       
   </head>
   
   <body>
       
               
        <div id="delcta" style="display: block;">
           <p>Time left for Free Next Day Delivery: <span id="countdowntimer"></span></p>
        </div>
       
         
       
        <script type="text/javascript">
         $('#countdowntimer').countdown('2017/10/13 15:57:00').on('update.countdown', function(event) {
            var $this = $(this).html(event.strftime(''
               + '%-H %!H:hour,hours; '
               + '%-M %!M:minute,minutes; '
               + '%-S %!S:second,seconds;'
            ));
         });
      </script>
   
   </body>
   
</html>


JS


Code:
/*!
 * The Final Countdown for jQuery v2.0.4 (http://hilios.github.io/jQuery.countdown/)
 * Copyright (c) 2014 Edson Hilios
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
!function(a){"use strict";"function"==typeof define&&define.amd?define(["jquery"],a):a(jQuery)}(function(a){"use strict";function b(a){if(a instanceof Date)return a;if(String(a).match(g))return String(a).match(/^[0-9]*$/)&&(a=Number(a)),String(a).match(/\-/)&&(a=String(a).replace(/\-/g,"/")),new Date(a);throw new Error("Couldn't cast `"+a+"` to a date object.")}function c(a){return function(b){var c=b.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);if(c)for(var e=0,f=c.length;f>e;++e){var g=c[e].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/),i=new RegExp(g[0]),j=g[1]||"",k=g[3]||"",l=null;g=g[2],h.hasOwnProperty(g)&&(l=h[g],l=Number(a[l])),null!==l&&("!"===j&&(l=d(k,l)),""===j&&10>l&&(l="0"+l.toString()),b=b.replace(i,l.toString()))}return b=b.replace(/%%/,"%")}}function d(a,b){var c="s",d="";return a&&(a=a.replace(/(:|;|\s)/gi,"").split(/\,/),1===a.length?c=a[0]:(d=a[0],c=a[1])),1===Math.abs(b)?d:c}var e=100,f=[],g=[];g.push(/^[0-9]*$/.source),g.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source),g.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source),g=new RegExp(g.join("|"));var h={Y:"years",m:"months",w:"weeks",d:"days",D:"totalDays",H:"hours",M:"minutes",S:"seconds"},i=function(b,c,d){this.el=b,this.$el=a(b),this.interval=null,this.offset={},this.instanceNumber=f.length,f.push(this),this.$el.data("countdown-instance",this.instanceNumber),d&&(this.$el.on("update.countdown",d),this.$el.on("stoped.countdown",d),this.$el.on("finish.countdown",d)),this.setFinalDate(c),this.start()};a.extend(i.prototype,{start:function(){null!==this.interval&&clearInterval(this.interval);var a=this;this.update(),this.interval=setInterval(function(){a.update.call(a)},e)},stop:function(){clearInterval(this.interval),this.interval=null,this.dispatchEvent("stoped")},pause:function(){this.stop.call(this)},resume:function(){this.start.call(this)},remove:function(){this.stop(),f[this.instanceNumber]=null,delete this.$el.data().countdownInstance},setFinalDate:function(a){this.finalDate=b(a)},update:function(){return 0===this.$el.closest("html").length?void this.remove():(this.totalSecsLeft=this.finalDate.getTime()-(new Date).getTime(),this.totalSecsLeft=Math.ceil(this.totalSecsLeft/1e3),this.totalSecsLeft=this.totalSecsLeft<0?0:this.totalSecsLeft,this.offset={seconds:this.totalSecsLeft%60,minutes:Math.floor(this.totalSecsLeft/60)%60,hours:Math.floor(this.totalSecsLeft/60/60)%24,days:Math.floor(this.totalSecsLeft/60/60/24)%7,totalDays:Math.floor(this.totalSecsLeft/60/60/24),weeks:Math.floor(this.totalSecsLeft/60/60/24/7),months:Math.floor(this.totalSecsLeft/60/60/24/30),years:Math.floor(this.totalSecsLeft/60/60/24/365)},void(0===this.totalSecsLeft?(this.stop(),this.dispatchEvent("finish")):this.dispatchEvent("update")))},dispatchEvent:function(b){var d=a.Event(b+".countdown");d.finalDate=this.finalDate,d.offset=a.extend({},this.offset),d.strftime=c(this.offset),this.$el.trigger(d)}}),a.fn.countdown=function(){var b=Array.prototype.slice.call(arguments,0);return this.each(function(){var c=a(this).data("countdown-instance");if(void 0!==c){var d=f[c],e=b[0];i.prototype.hasOwnProperty(e)?d[e].apply(d,b.slice(1)):null===String(e).match(/^[$A-Z_][0-9A-Z_$]*$/i)?(d.setFinalDate.call(d,e),d.start()):a.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi,e))}else new i(this,b[0],b[1])})}});


That page all works, if I then look at the smarty page in question I see this is highlighted as an error :-

Code:
(index):990 Uncaught TypeError: $(...).countdown is not a function
    at (index):990
(anonymous) @ (index):990


So I guess that means the JS is the issue but the question is why only on the Smarty page version & not the PHP page I made to test it all?
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 118
Location: West Coast

PostPosted: Thu Oct 12, 2017 4:52 pm    Post subject: Reply with quote

From how you say you are conducting your experiments, I infer that you have added the Smarty code to an already existing page in your application, but the verifications you made were with a custom standalone PHP page.

If this is correct, then I would start looking at the browser's diagnostic Network tab that shows when all of the page's javascript resources were called in.

It could be the case that the script that calls the countdown function is executing (being inline with the HTML code) before the browser fetches the javascript file that defines the function.
Back to top
View user's profile Send private message
drgl
Smarty Rookie


Joined: 06 Oct 2017
Posts: 17

PostPosted: Thu Oct 12, 2017 5:10 pm    Post subject: Reply with quote

So it could be just a fact that the code needs pasting elsewhere? The PHP page was indeed just a stand alone test page. Thanks again for all of your help & time.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Help Wanted (commercial) All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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