NAME

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

DESCRIPTION

Build a static page

Create a controller method that looks like this:

    sub help {
        my ($self, $m) = @_;
        $m->comp("view/something/help.mhtml")
    }

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:

    <%args>
    %details
    </%args>

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

    <p>
    Description: <% $details{description} %>
    </p>

Pick up data from the URL

In your controller, do:

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

If the user requested the URL http://example.com/article/view/foo/bar/baz 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");
            $m->comp("view/book/search_form.mhtml");
        }
    }

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:

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

SEE ALSO

MasonX::MiniMVC