将变量传递给 FuelPHP 框架中的模板.php文件


Passing a variable to the template.php file in FuelPHP Framework

我对FuelPHP和PHP/框架非常陌生,并设法设置了所有内容并运行了一些控制器等。但是我不确定如何将我设置的变量传递给模板.php文件。

应用/视图/模板.php

有问题的部分:

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

应用/类/控制器/仪表板.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);
    }
}

应用/类/视图/仪表板/索引.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

有什么想法吗?

还有一件事,是否可以添加部分,例如在菜单、页眉、页脚等中加载?

只需在里面添加另一个属性:

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

是的,您也可以添加其他属性。