0

I have a smarty template that has been working fine for me for some time but for reasons I don't quite understand one of the variables that I "assign" to the template is $activity_date and when I send in time information with my date information it has some surprising effects (aka, it works when "2012-12-27" is passed in but falls over in a bizzare way when "2012-12-27 00:00:00" is passed in.

It might make a little more sense if the problems actually were related to some sort of template interaction with this variable but it doesn't appear to be. Instead the end effect is that conditional statements like:

class="ajax-selector {if ($span)}span{$span}{/if} left" 

produce the following error:

PHP Notice:  Undefined index: span in /[PATH]/templates/compiled/96681eebf17717b8605c8deffb547d7e89c1ace6.file.select2-ajax.tmpl.php on line 35

To me this is quite paradoxical. The code says "test for the existance of $span" and then take action only if it does exist. The error message complains about an undefined index which was the intent of the conditional test.

As I said earlier, all of these types of conditional statements used to work exactly as you'd expect them to but they've all started failing and the only difference seems to be whether the "activity_date" comes back with time-level precision or just date-level precision.

In case it is helpful, here's the code that sets up the template:

public function handler ( $request ) {
    $response = new stdClass();
    $response->action = $request['action'];
    $response->post_type = LG_TAX_Action()->post_type($response->action); 
    $response->meta = LG_TAX_Action()->meta ( $response->action );

    $template = FE_Template::init();
    $meta = new LG_Meta();
    $template->assign ( 'form' , $response );
    if ( isset ($request['activity_date']) ) {
        $template->assign ( 'activity_date' , $request['activity_date'] );
    }
    $named_days = new stdClass();
    $named_days->today = new DateTime('today');
    $named_days->today = $named_days->today->format('Y-m-d');
    $named_days->yesterday = new DateTime('yesterday');
    $named_days->yesterday = $named_days->yesterday->format('Y-m-d');
    $template->assign ( 'named_days' , $named_days ); 
    $template->assign ( 'meta' , $meta ); // provides the "template_exists()" method

    $response->template = $template->fetch( 'activity_forms/master.tmpl' );

    return $response;
}
ken
  • 8,763
  • 11
  • 72
  • 133
  • I'm not familiar with Smarty, but would a `if(isset($span))` or `if(!empty($span))` work? Just a shot in the dark.... – Major Productions Dec 28 '12 at 01:07
  • 1
    Yes I was tempted to do that (aka, the PHP way) but in the Smarty examples I'd seen before it was always suggested to do it the way I have in my code above. That said, it's still worth giving it a try ... let's see how it goes. – ken Dec 28 '12 at 01:10
  • 1
    It does seem to improve the situation. I'm going to get some sleep and see if becomes clearer in the morning. – ken Dec 28 '12 at 01:21
  • Yeah, like I said, it was a shot in the dark as I use Twig instead of Smarty. Good luck! :) – Major Productions Dec 28 '12 at 01:42

1 Answers1

0

Here is what I think could be causing the problem.

From the code you posted above, $span does not appear to be init/set anywhere. This obviously will cause an undefined index error.But perhaps the smarty objects has not been set up to ignore those undefined error reporting. You can easily do that by passing an array key "error_reporting" with value "YES" to the constructor of smarty during instantiation or setOption method.

Also if you are using a smarty's date format method to generate the $span, then according to the smarty documentation , the value should be

unix timestamps, mysql timestamps or any string made up of month day year, parsable by php's strtotime()

Hope it helps!

shawndreck
  • 2,039
  • 1
  • 24
  • 30
  • $span is set by the calling template but in some cases it is NOT set which is why I have the conditional logic around it. That said, while a simple {if ($span)} used to work fine to test for whether the variable was set it seems to be temperamental in some cases so while I like it's compact style the code {if (isset($span))} is more robust. – ken Dec 28 '12 at 23:11