SMARTY Fatal Error

Discussion in 'PHP' started by Louis11, Jul 31, 2008.

  1. #1
    I have been tinkering with SMARTY the last few days, and so far have had little problems. I am moderately familiar with the setup and have read through several articles found through Google as well as the documentation provided on the SMARTY website. With that said, I am having some problems with my SMARTY setup.

    I posted this question in the SMARTY forums (http://www.phpinsider.com/smarty-forum/viewtopic.php?p=51778#51778), but so far no one has been able to help :(

    The error in question is:
    City.php contains:
    <?
    // include configuration files
    require_once('includes/configuration.php');
       
    global $smarty;
    
    // get action
    $action = strip_tags($_GET['action']);
       
    // add new city
    if($action == 'add')
    {
       $smarty -> assign('sub_title', 'Add City');
       $smarty -> display('pages/city_add.tpl');
    }
       
    // modify existing city
    else if($action == 'modify')
    {
       $smarty -> assign('sub_title', 'Modify City');
       $smarty -> display('pages/city_modify.tpl');
    }
       
    // remove city
    else if($action == 'remove')
    {
       $smarty -> assign('sub_title', 'Remove City');
       $smarty -> display('pages/city_remove.tpl');
    }
       
    // show navigation
    else
    {
       $smarty -> assign('sub_title', 'City Management');
       $smarty -> display('pages/city.tpl');
    }
    ?> 
    PHP:

    and the configuration file required:
    <?
    // include all library files
    require_once('libs/Smarty.class.php');
       
    // setup SMARTY
    global $smarty;
    $smarty = new smarty;
    $smarty -> template_dir = 'templates';
    $smarty -> compile_dir  = 'templates_c';
    $smarty -> cache_dir    = 'cache';
    $smarty -> config_dir   = 'configs';
    $smarty -> security = true;
    ?> 
    PHP:

    I've tried several variations in setting up the smarty resource, but still no good. I added the global $smarty (as I was told to do in the SMARTY forums) and still nothing . . . I even verified that globals where turned on in my php.ini file.

    The configuration file is in the correct directory. I verified this multiple times, and even changed the require_once to some arbitrary file name, which produced an error.

    I even went as far as to verify that the SMARTY object was being created by doing a print_r($smarty). With and without globals turned on (or specified in the code) it gave the smarty object correctly. So I know the object is being created, and passed from the configuration file.

    This is absolutely driving me nuts, any help would be greatly appreciated!
     
    Louis11, Jul 31, 2008 IP
  2. PET

    PET Member

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Which exactly is the line 41? :)

    By the way. Short sugestion.

    Don't use IF... ELSEIF.... and so on.

    Try using SWITCH... it's much more elegant in this case.

    
    switch($_GET['act']){
    		
    	case '':
    			
    	$smarty->assign('page_title', 'Admin Page');
    	$smarty->display('admin.tpl');
    	break;
    
                case 'modify':
    
    echo "something";
    break;
    }
    
    PHP:
    Also... what exactly is this: global $smarty;?

    Try : $smarty = new Smarty();
     
    PET, Jul 31, 2008 IP
  3. mexabet

    mexabet Well-Known Member

    Messages:
    866
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Try using include like this:
    $smarty->assign('page_title', 'Admin Page');
    include('pages/city_modify.tpl');
    PHP:
    instead of

    $smarty -> display('pages/city_modify.tpl');
    PHP:
     
    mexabet, Jul 31, 2008 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    There are no evident errors here. Plese re-check that you include the correct config file. Try to temporary rename 'includes/configuration.php'. Also, try to var_dump($smarty) at the beginning of City.php.
     
    wmtips, Jul 31, 2008 IP
  5. Typo3 Steve

    Typo3 Steve Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5


    Don't do what this guy tells you to do... you are right to use display. The display function in smarty fetch the content of the template, and show it to the user (echo).
     
    Typo3 Steve, Jul 31, 2008 IP
  6. Typo3 Steve

    Typo3 Steve Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    In your City.php, can you add this code just under the global $smarty;

    print_r($smarty);

    I want to see what is outputted.

    Steve
     
    Typo3 Steve, Jul 31, 2008 IP
  7. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Yea I know :p that's the whole reason i'm using SMARTY.

    The SMARTY object is instated in configuration.php. The global is supposed to make it accessible in the other files (i.e. city.php). I was told to try this by the Administrator on the SMARTY forums. Didn't work, but at this point I figured it couldn't hurt.

    Also, I tried doing a dump on the smarty object. I changed city.php to:
    
    <?
    	// include configuration files
    	require_once('includes/configuration.php');
    	
    	global $smarty;
    	
    	print_r($smarty);
    ?>
    
    PHP:
    and it prints out:
    Also to verify that it is accessing the configuration file correctly, I placed a print at the top of my configuration file. And after removing all of the smarty calls from city.php it prints out what was placed in the configuration file. So I know that it's accessing the configuration file correctly. Just don't know what is wrong with SMARTY.

    I also thought that maybe the smarty library had gotten corrupted, so I redownloaded the files (twice) and still nothing . . . :(
     
    Louis11, Jul 31, 2008 IP
  8. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    To reiterate what PET said above :

    Which exactly is the line 41?
    b/c there are not 41 lines in that code snippit

    This will help alot!
     
    rob_v, Jul 31, 2008 IP
  9. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #9
    I had some comments at the top of city.php and removed them before posting (figured no one would want to muddle through notes I wrote to myself :p). In either case it's just an assign call to the smarty object, particularly:
    $smarty -> assign('sub_title', 'City Management');
    PHP:
    Someone in the SMARTY forums suggested that I do a:
    class mysmarty
    	{
    	  public static function &instance()
    	  {
    		static $smarty_obj = null;
    		if(!isset($smarty_obj))
    		{
    		  // include all library files
    		  require_once('libs/Smarty.class.php');
    		  
    		  // setup SMARTY
    		  $smarty_obj = new Smarty();
    		  $smarty_obj -> template_dir = 'templates';
    		  $smarty_obj -> compile_dir  = 'templates_c';
    		  $smarty_obj -> cache_dir    = 'cache';
    		  $smarty_obj -> config_dir   = 'configs';
    		  $smarty_obj -> security = true;
    		}
    		return $smarty_obj;
    	  }
    	} 
    PHP:
    in my configuration file and then call it by:
    $smarty = mysmarty::instance();
    PHP:
    in all subsequent PHP files (i.e. city.php). This resolves the problem, however now regardless of what template I display, three templates are shown (customer, city, and account). I assume if I can figure out why it calls all of these templates, then I could also figure out why the error on city.php shows up on all PHP scripts in my web app.
     
    Louis11, Jul 31, 2008 IP
  10. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #10
    Very strange, smarty object seems to be ok.
    Strange thing I noticed: your error message states error on line 41, but initial city.php you provided contains only 36 lines. Maybe you have several city.php files in your search path? Also, if your file system is case-sensitive, City.php and city.php are the different filenames.
     
    wmtips, Jul 31, 2008 IP
  11. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #11
    Yea, as aforementioned there are 5 lines of comments at the top of city.php that I didn't post with my initial post :)
     
    Louis11, Jul 31, 2008 IP
  12. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #12
    SOLVED!
    Well the issue has been solved. I am not sure exactly what the issue was, but in either case it has been resolved. For anyone that might run across this problem in the future here is what I did (or rather, what was recommended of me):

    Instead of initiating the object directly in my configuration file I created another class called 'mysmarty' that creates the smarty object directly in one of mysmarty's functions. You then pass this object by reference to all of your subsequent pages. Here is the code I used:

    
    class mysmarty
    	{
    		public static function &instance()
    		{
    			static $smarty_obj = null;
    			
    			if(!isset($smarty_obj))
    			{
    				// include all library files
    				require_once('libs/Smarty.class.php');
    				
    				// setup SMARTY
    				$smarty_obj = new Smarty();
    				$smarty_obj -> template_dir = 'templates';
    				$smarty_obj -> compile_dir  = 'templates_c';
    				$smarty_obj -> cache_dir    = 'cache';
    				$smarty_obj -> config_dir   = 'configs';
    				$smarty_obj -> security = true;
    			}
    			return $smarty_obj;
    		}
    	}
    
    PHP:
    Now in any file you need to call smarty do a:
    
    $smarty = mysmarty::instance();
    
    PHP:
    That should resolve the problem :)

    Thanks to everyone who helped out!
     
    Louis11, Jul 31, 2008 IP
  13. strikefreedom

    strikefreedom Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    thanks too, for resolve this problem
     
    strikefreedom, Jul 31, 2008 IP