简体   繁体   English

将变量从控制器传递到视图

[英]Passing variables from controller to views

I'm using kohana 3.3 together with kostache. 我正在与kostache一起使用kohana 3.3。 Ok how do you pass a variable from controller to class views and use it on a mustache file?.. 好了,如何将变量从控制器传递到类视图,并在髭文件上使用它?

Here's my code. 这是我的代码。

Controller: 控制器:

public function action_update()
{
    $layout = Kostache_Layout::factory();

    $content = new View_Pages_Album_Update();

    $album = ORM::factory('Album_Information', $this->request->post('id'));

    $content->albumName = $album->Album_Name;

    $content->albumArtist = $album->Artist;

    $this->response->body($layout->render($content));
}

Views 查看

class View_Pages_Album_Update {

    public $title = 'Update Music';

    public function album_edit()
    {
        return array(
                    array('albumName' => $this->albumName),
                    array('albumArtist' => $this->albumArtist),
        );
    }
}

Template 模板

<label>Album Name:</label>

<input type="text" name="inputAlbumName" value="{{albumName}}" /><br />

<label>Artist:</label>
<input type="text" name="inputArtist" value="{{albumArtist}}" /><br />

When I run my code nothing is passed to the template file. 当我运行代码时,没有任何内容传递给模板文件。 So how do you pass it from controller => views => template? 那么如何从控制器=>视图=>模板传递它呢?

You are setting values on the Kostache_Layout object. 您正在Kostache_Layout对象上设置值。 I believe you must set those on the View_Pages_Album_Update instead: 我相信您必须在View_Pages_Album_Update上进行设置:

Controller: 控制器:

public function action_update()
{
    $layout = Kostache_Layout::factory();
    $content = new View_Pages_Album_Update();

    $album = ORM::factory('Album_Information', $this->request->param('id'));

    $content->albumName = $album->Album_Name;
    $content->albumArtist = $album->Artist;

    $this->response->body($layout->render($content));
}

I don't think you need a album_edit() method for the template you showed, but if you think you do, then use this. 我认为您不需要为显示的模板使用album_edit()方法,但是如果您认为需要,请使用此方法。

View: 视图:

class View_Pages_Album_Update { 类View_Pages_Album_Update {

public $title = 'Update Music';

public function album_edit()
{
    return array(
                array('albumName' => $this->albumName), // missing $this
                array('albumArtist' => $this->albumArtist), // missing $this
    );
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM