0

I'm trying to do some OOP along with Smarty. When i put, for example

$smarty->display('header.tpl');

in a construct function, everything works. However, when I put this code in a function and call the function in the construct, nothing happens.

Is there a solution so I can use the code in a function and then call it in a function?

init.php code:

class init{
    public function __construct(){

    $smarty = new Smarty();
    $smarty->setTemplateDir('templates');
    $smarty->setCompileDir('templates_c');
    $smarty->setCacheDir('cache');
    $smarty->setConfigDir('configs');
    //$smarty->testInstall();

    $smarty->display('header.tpl');
    $smarty->display('content.tpl');
    $smarty->display('footer.tpl');

    //-----------------------------------
    //-----------------------------------
    //check url
    //-----------------------------------
    //-----------------------------------

    if(isset($_REQUEST['params']) && $_REQUEST['params']!=''){
        $aParams = explode('/', $_REQUEST['params']);
        print_r ($aParams);
        if(isset($aParams[0])) $this->lang = $aParams[0];
        if(isset($aParams[1])) $this->page = $aParams[1];
    }

    if(!$this->lang) $this->lang = 'nl';
    if(!$this->page) $this->page = 'home';

    //-----------------------------------
    //-----------------------------------
    //Functions
    //-----------------------------------
    //-----------------------------------

    $this->buildPage();
    $this->buildHeader_Footer();

}

function buildPage(){
    require_once('modules/' . $this->page . '/' . $this->page . '.php');
    if($this->page == 'home') new home($this->lang, $this->page, $this->action, $this->id, $this->message);
    else if($this->page == 'contact') new contact($this->lang, $this->page, $this->action, $this->id, $this->message);

}

function buildHeader_Footer(){
    $smarty->display('header.tpl');
    $smarty->display('footer.tpl');
}

}

Index.php code:

require('smarty/libs/Smarty.class.php');

require_once ('modules/init/init.php'); 
$init = new init();
tereško
  • 58,060
  • 25
  • 98
  • 150
Axll
  • 55
  • 6

1 Answers1

0

Update (after question has changed)

It seems that you expect that the $smarty variable is accessible from all class method after creation in constructor. That's wrong. A class variable has the be accessed using $this inside the class. So you would have to write:

$this->smarty-> ...

whenever using it.


I cannot say what exactly is the problem with your solution as the posted code is incomplete. But what you are trying to do should work.

As an example, I would have a class like this:

class SimpleView {

    /**
     * Note that smarty is an instance var. This means that var
     * is only available via `$this` in the class scope
     */
    protected $smarty;


    /**
     * Constructor will be called if write 'new SimpleView()'
     */
    public function __construct(){
        // note $this
        $this->smarty = new Smarty();
        $this->smarty->setTemplateDir('templates');
        $this->smarty->setCompileDir('templates_c');
        $this->smarty->setCacheDir('cache');
        $this->smarty->setConfigDir('configs');
    }


    /**
     * The build function is public. It can be called from 
     * outside of the class
     */
    public function build(){
        $this->smarty->display('header.tpl');
        $this->smarty->display('content.tpl');
        $this->smarty->display('footer.tpl');
    }
}

and use it like this:

$view = new SimpleView();
$view->build();
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • I edited the code. I tried to make the changes as you suggested but it didn't seem to work. – Axll Feb 13 '13 at 18:38
  • I see no `class` construct in your code. Also, as I told: **note** the `$this`. – hek2mgl Feb 13 '13 at 18:40
  • Forgot to copy that with the last edit. I'll try to add the $this – Axll Feb 13 '13 at 18:46
  • I just tried to add $this but it only gives an error and nothing is displayed on the page. – Axll Feb 13 '13 at 18:54
  • [**Learn howto display errors**](http://stackoverflow.com/questions/5680831/php-does-not-display-error-messages) – hek2mgl Feb 13 '13 at 19:34
  • I made a new file with the code you provided and it doesn't seem to work. I disabled the errors in the php.ini file and with php itself. The error i get: HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request. – Axll Feb 13 '13 at 21:48