2013-02-13 2 views
0

Smarty와 함께 일부 OOP를 수행하려고합니다. 예를 들어, 넣을 때PHP 함수의 Smarty

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

을 구성 함수로 사용하면 모든 것이 작동합니다. 그러나,이 코드를 함수에 넣고 함수에서 함수를 호출하면 아무 일도 일어나지 않습니다.

함수에 코드를 사용하고 함수에서 호출 할 수있는 솔루션이 있습니까?

init.php 코드 :

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는 코드 : (질문 변경 후)

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

require_once ('modules/init/init.php'); 
$init = new init(); 

답변

0

업데이트

당신이 $smarty 변수에 액세스 할 수 있는지 기대하는 것 생성자에서 생성자로 모든 클래스 메서드에서. 그건 틀렸어요. 클래스 변수는 클래스 내에 $this을 사용하여 액세스 할 수 있습니다. 따라서 쓸 때마다 :

$this->smarty-> ... 

을 써야합니다.


게시 된 코드가 완전하지 않으므로 솔루션의 문제점을 정확히 말할 수 없습니다. 그러나 당신이하려는 것은 효과가 있습니다.

는 예를 들어,이 같은 클래스 것 :

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'); 
    } 
} 

과 같이 사용 :

$view = new SimpleView(); 
$view->build(); 
+0

나는 코드를 편집했다. 내가 제안한대로 변경하려고했지만 작동하지 않는 것 같습니다. – Axll

+0

코드에'class' 구조가 없습니다. 또한, 내가 말한대로 : ** 참고 **'$ this'. – hek2mgl

+0

마지막 편집 내용으로 복사하는 것을 잊어 버렸습니다. 나는이 $ – Axll