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!
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();
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:
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.
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).
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
Yea I know 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 . . .
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!
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 ). 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.
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.
Yea, as aforementioned there are 5 lines of comments at the top of city.php that I didn't post with my initial post
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!