dqdz6464 2013-12-09 09:04
浏览 15
已采纳

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

I'm using kohana 3.3 together with kostache. 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?

  • 写回答

1条回答 默认 最新

  • dongyunque2511 2013-12-09 11:33
    关注

    You are setting values on the Kostache_Layout object. I believe you must set those on the View_Pages_Album_Update instead:

    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.

    View:

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

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失