Passing variables from controller to views

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?

Answers


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

}


Need Your Help

Compatibility Issue with IE10 and IE11

jsp internet-explorer cross-browser applet compatibility

I have a website that having some applets, which works without any issues in IE6-IE9. But when I tried in IE10 and IE11, it seems to be buggy. All the pages where not loading properly.

Dynamic Programing : Rod cutting implementation error

c++ algorithm dynamic-programming

Given a rod of length n inches and a table of prices pi for

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.