【问题标题】:Passing a variable to the template.php file in FuelPHP Framework将变量传递给 FuelPHP 框架中的 template.php 文件
【发布时间】:2016-07-13 22:46:43
【问题描述】:

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

app/views/template.php

有问题的部分:

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

app/classes/controller/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);
    }
}

app/classes/view/dashboard/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

有什么想法吗?

另外一件事,是否可以在菜单、页眉、页脚等中添加部分内容?

【问题讨论】:

    标签: php fuelphp


    【解决方案1】:

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

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

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

    【讨论】:

    • 我在视图或控制器中添加它?谢谢你的帮助
    • @James 我通常将它添加到从Controller_Template 扩展的控制器中,所以在你的情况下将它添加到Controller_Dashboard
    • @James 这里是例子fuelphp.com/docs/general/controllers/template.html 我也是这样做的,在类的属性上声明模板文件,然后在需要时分配$this-&gt;template-&gt;whatever
    • 酷我试了一下,它在登录页面上工作,我设置了一个不同的模板,但是当它登录时只显示

      testing

      而不是使用默认模板,所以试图工作这样我就可以看到它工作了哈哈
    • @James 是的,当然是不同的模板,然后是不同的属性,如果它们共享相同的模板,那么它将在所有视图中使用,很高兴这有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多