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

    }

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?