2016-07-13 74 views
1

我對FuelPHP和PHP/frameworks一般都很陌生,並設法讓一切設置和一些控制器運行等。但是我不知道如何傳遞一個變量我設置爲template.php文件。將變量傳遞給FuelPHP框架中的template.php文件

應用/視圖/的template.php

部分中的問題:

<body class="<?php echo $bodyClass; ?>"> 

應用程序/類/控制器/ dashboard.php

class Controller_Dashboard extends Controller_Template 
{ 

    /** 
    * The basic welcome message 
    * 
    * @access public 
    * @return Response 
    */ 
    public function action_index() 
    { 
     return Response::forge(View::forge('dashboard/index')); 
    } 

    /** 
    * The 404 action for the application. 
    * 
    * @access public 
    * @return Response 
    */ 
    public function action_404() 
    { 
     return Response::forge(Presenter::forge('dashboard/404'), 404); 
    } 
} 

應用程序/類/視圖/儀表板/ index.php

class View_Dashboard_Index extends ViewModel 
{ 
    /** 
    * Prepare the view data, keeping this in here helps clean up 
    * the controller. 
    * 
    * @return void 
    */ 
    public function view() 
    { 
     $this->template = $this->request()->param('bodyClass', ''); 
    } 
} 

我相信我得到它錯了,很明顯,我是因爲它不工作,我得到:

Fuel\Core\PhpErrorException [ Notice ]: 
Undefined variable: bodyClass 

C:/Websites/Annilla/Clients/Four04 Packaging/DEV/NEW/BUILD/backend/app/views/template.php @ line 24 

任何想法?

另外還有一件事,是否有可能像在菜單,頁眉,頁腳等加載部分?

回答

2

只是裏面添加另一個屬性:

$this->template->bodyClass = 'container'; // just assign 

是的,你可以添加其他的屬性也是如此。

+0

我補充說,在視圖或控制器?感謝您的幫助順便說一句 – James

+0

@詹姆斯我通常將它添加到從'Controller_Template'擴展的控制器中,所以在您的情況下將其添加到'Controller_Dashboard'內 – Ghost

+0

@James這裏是示例http://fuelphp.com/docs/general /controllers/template.html這就是我所做的,在類的屬性上聲明模板文件,然後在需要時分配'$ this-> template-> whatever' – Ghost