View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Alex Skud Bayley > MasonX-MiniMVC > MasonX::MiniMVC::Cookbook



Annotate this POD

View/Report Bugs


MasonX::MiniMVC::Cookbook -- examples of MiniMVC usage


Build a static page

Create a controller method that looks like this:

    sub help {
        my ($self, $m) = @_;

Your help.mhtml view file will (presumably) contain only static HTML.

Pass data to a dynamic page

Create a controller method that does most of the work of figuring out the details, then passes a pile o' data to the view for display:

    sub details {
        my ($self, $m, $id) = @_;
        # let's pretend fetch() gives us a hashref...
        my $details = MyApp::Model::Whatever->fetch($id);
        $m->comp("view/something/details.mhtml", details => $details);

The view looks something like this:


    <h1>Details for <% $details{title} %></h1>

    Description: <% $details{description} %>

Pick up data from the URL

In your controller, do:

    sub view {
        my ($self, $m, @args) = @_;
        # ...

If the user requested the URL and the controller for "article" is MyApp::Article, then this will call MyApp::Article::view() with @args set to ("foo", "bar", "baz").

Use data from a form submission

In your controller, do:

    sub add {
        my ($self, $m, @args) = @_;
        my %fields = $m->request_args();

If you want a single form field, you can use $m-request_args->{$field}>.

Use data from either a form or a URL

Here's an example of combining the previous two techniques, taken from the examples/library demo included with the MiniMVC distro.

    sub search {
        my ($self, $m, @args) = @_;
        $m->notes("title", "MiniMVC Library Demo: Search Results");
        if (my $query = $m->request_args->{query}) { # search by form
            $m->comp("view/book/search_results.mhtml", query => $query);
        } elsif (@args) { # search by URL
            $m->comp("view/book/search_results.mhtml", query => join(" ", @args));
        } else {
            $m->notes("title", "MiniMVC Library Demo: Search");

Change the overall look and feel

Edit the autohandler file to include whatever HTML you want.

Add dynamic content (such as page title) to the autohandler

Use Mason's notes() facility:

    $m->notes("key", "value");
    $m->notes("title", "Details for item $id");

Then, in the autohandler, do something like:

        <title><% $m->notes("title") || "My Application" %></title>



syntax highlighting: