SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast louis11's Avatar
    Join Date
    Jul 2004
    Location
    United States of America
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy SMARTY Fatal Error

    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-for...?p=51778#51778), but so far no one has been able to help

    The error in question is:
    Fatal error: Call to a member function assign() on a non-object in /Users/city.php on line 41
    City.php contains:
    PHP Code:
    <?
    // 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');
    }
    ?>

    and the configuration file required:
    PHP Code:
    <?
    // 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;
    ?>

    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!

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just as a test, stick

    Code php:
    print_r($smarty->get_config_vars());

    at the top under global $smarty.


  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not an expert here but I have been working with smarty for a few months now. I think it has to do with how you are instantiating the smarty object. I would try changing the config file to this.

    PHP Code:
    // include all library files
    require_once('libs/Smarty.class.php');
     
    // setup SMARTY
    class setup extends Smarty
    {
          function 
    setup()
          {
          
    $this->Smarty();
          
    $smarty -> template_dir 'templates';
          
    $smarty -> compile_dir  'templates_c';
          
    $smarty -> cache_dir    'cache';
          
    $smarty -> config_dir   'configs';
          
    $smarty -> security true;
          }

    now on City.php replace

    PHP Code:
    global $smarty
    with

    PHP Code:
    $smarty = new setup
    Let me know if this works for you.

  4. #4
    SitePoint Enthusiast louis11's Avatar
    Join Date
    Jul 2004
    Location
    United States of America
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    just as a test, stick

    Code php:
    print_r($smarty->get_config_vars());

    at the top under global $smarty.
    I just did a print_r($smarty); and here is what it produces:

    Smarty Object ( [template_dir] => templates [compile_dir] => templates_c [config_dir] => configs [plugins_dir] => Array ( [0] => plugins ) [debugging] => [error_reporting] => [debug_tpl] => [debugging_ctrl] => NONE [compile_check] => 1 [force_compile] => [caching] => 0 [cache_dir] => cache [cache_lifetime] => 1800 [cache_modified_check] => [php_handling] => 0 [security] => 1 [secure_dir] => Array ( ) [security_settings] => Array ( [PHP_HANDLING] => [IF_FUNCS] => Array ( [0] => array [1] => list [2] => isset [3] => empty [4] => count [5] => sizeof [6] => in_array [7] => is_array [8] => true [9] => false [10] => null ) [INCLUDE_ANY] => [PHP_TAGS] => [MODIFIER_FUNCS] => Array ( [0] => count ) [ALLOW_CONSTANTS] => ) [trusted_dir] => Array ( ) [left_delimiter] => { [right_delimiter] => } [request_vars_order] => EGPCS [request_use_auto_globals] => 1 [compile_id] => [use_sub_dirs] => [default_modifiers] => Array ( ) [default_resource_type] => file [cache_handler_func] => [autoload_filters] => Array ( ) [config_overwrite] => 1 [config_booleanize] => 1 [config_read_hidden] => [config_fix_newlines] => 1 [default_template_handler_func] => [compiler_file] => Smarty_Compiler.class.php [compiler_class] => Smarty_Compiler [config_class] => Config_File [_tpl_vars] => Array ( [SCRIPT_NAME] => /~louislang/Admin Panel/city.php [clock] => It is 2:29 on Jul 31, 2008 [profits] => 40.54 ) [_smarty_vars] => [_sections] => Array ( ) [_foreach] => Array ( ) [_tag_stack] => Array ( ) [_conf_obj] => [_config] => Array ( [0] => Array ( [vars] => Array ( ) [files] => Array ( ) ) ) [_smarty_md5] => f8d698aea36fcbead2b9d5359ffca76f [_version] => 2.6.19 [_inclusion_depth] => 0 [_compile_id] => [_smarty_debug_id] => SMARTY_DEBUG [_smarty_debug_info] => Array ( ) [_cache_info] => Array ( ) [_file_perms] => 420 [_dir_perms] => 505 [_reg_objects] => Array ( ) [_plugins] => Array ( [modifier] => Array ( ) [function] => Array ( ) [block] => Array ( ) [compiler] => Array ( ) [prefilter] => Array ( ) [postfilter] => Array ( ) [outputfilter] => Array ( ) [resource] => Array ( ) [insert] => Array ( ) ) [_cache_serials] => Array ( ) [_cache_include] => [_cache_including] => )
    I also tried the method proposed by binarysys and it said that it couldn't find the class 'Setup'. So I thought that maybe it can't find the configuration file. So I placed a:
    PHP Code:
    print 'Works!'
    at the top of my config file, and sure enough when I access city.php (after removing the smarty calls) it prints out 'Works!'. So I assumed it's accessing the config file correctly.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I left out a an important part there you have to put a require statement right before you instantiate the setup.

    PHP Code:
    require_once('config.php'); // path to config
    $smarty = new setup
    It should work now.

  6. #6
    SitePoint Enthusiast louis11's Avatar
    Join Date
    Jul 2004
    Location
    United States of America
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by binarysys View Post
    I left out a an important part there you have to put a require statement right before you instantiate the setup.

    PHP Code:
    require_once('config.php'); // path to config
    $smarty = new setup
    It should work now.
    Yea someone else in the SMARTY forums recommened:
    PHP Code:
    <?php
    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;
      }
    }
    ?>
    and call by
    PHP Code:
    require_once('includes/configuration.php');
    $smarty mysmarty::instance(); 
    And that works, tentatively. Now, three templates are shown (city, account, customer) no matter what page you are on.

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    St. Louis, MO.
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing the "else if" statements to "if".


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •