| 1 | package MasonX::MiniMVC::Cookbook; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MasonX::MiniMVC::Cookbook -- examples of MiniMVC usage | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | =head2 Build a static page | ||
| 10 | |||
| 11 | Create a controller method that looks like this: | ||
| 12 | |||
| 13 | sub help { | ||
| 14 | my ($self, $m) = @_; | ||
| 15 | $m->comp("view/something/help.mhtml") | ||
| 16 | } | ||
| 17 | |||
| 18 | Your C<help.mhtml> view file will (presumably) contain only static HTML. | ||
| 19 | |||
| 20 | =head2 Pass data to a dynamic page | ||
| 21 | |||
| 22 | Create a controller method that does most of the work of figuring out | ||
| 23 | the details, then passes a pile o' data to the view for display: | ||
| 24 | |||
| 25 | sub details { | ||
| 26 | my ($self, $m, $id) = @_; | ||
| 27 | # let's pretend fetch() gives us a hashref... | ||
| 28 | my $details = MyApp::Model::Whatever->fetch($id); | ||
| 29 | $m->comp("view/something/details.mhtml", details => $details); | ||
| 30 | } | ||
| 31 | |||
| 32 | The view looks something like this: | ||
| 33 | |||
| 34 | <%args> | ||
| 35 | %details | ||
| 36 | </%args> | ||
| 37 | |||
| 38 | <h1>Details for <% $details{title} %></h1> | ||
| 39 | |||
| 40 | <p> | ||
| 41 | Description: <% $details{description} %> | ||
| 42 | </p> | ||
| 43 | |||
| 44 | =head2 Pick up data from the URL | ||
| 45 | |||
| 46 | In your controller, do: | ||
| 47 | |||
| 48 | sub view { | ||
| 49 | my ($self, $m, @args) = @_; | ||
| 50 | # ... | ||
| 51 | } | ||
| 52 | |||
| 53 | If the user requested the URL http://example.com/article/view/foo/bar/baz and | ||
| 54 | the controller for "article" is MyApp::Article, then this will call | ||
| 55 | C<MyApp::Article::view()> with C<@args> set to C<("foo", "bar", "baz")>. | ||
| 56 | |||
| 57 | =head2 Use data from a form submission | ||
| 58 | |||
| 59 | In your controller, do: | ||
| 60 | |||
| 61 | sub add { | ||
| 62 | my ($self, $m, @args) = @_; | ||
| 63 | my %fields = $m->request_args(); | ||
| 64 | } | ||
| 65 | |||
| 66 | If you want a single form field, you can use C<$m->request_args->{$field}>. | ||
| 67 | |||
| 68 | =head2 Use data from either a form or a URL | ||
| 69 | |||
| 70 | Here's an example of combining the previous two techniques, taken from the | ||
| 71 | C<examples/library> demo included with the MiniMVC distro. | ||
| 72 | |||
| 73 | sub search { | ||
| 74 | my ($self, $m, @args) = @_; | ||
| 75 | $m->notes("title", "MiniMVC Library Demo: Search Results"); | ||
| 76 | if (my $query = $m->request_args->{query}) { # search by form | ||
| 77 | $m->comp("view/book/search_results.mhtml", query => $query); | ||
| 78 | } elsif (@args) { # search by URL | ||
| 79 | $m->comp("view/book/search_results.mhtml", query => join(" ", @args)); | ||
| 80 | } else { | ||
| 81 | $m->notes("title", "MiniMVC Library Demo: Search"); | ||
| 82 | $m->comp("view/book/search_form.mhtml"); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | =head2 Change the overall look and feel | ||
| 87 | |||
| 88 | Edit the C<autohandler> file to include whatever HTML you want. | ||
| 89 | |||
| 90 | =head2 Add dynamic content (such as page title) to the autohandler | ||
| 91 | |||
| 92 | Use Mason's C<notes()> facility: | ||
| 93 | |||
| 94 | $m->notes("key", "value"); | ||
| 95 | $m->notes("title", "Details for item $id"); | ||
| 96 | |||
| 97 | Then, in the autohandler, do something like: | ||
| 98 | |||
| 99 | <head> | ||
| 100 | <title><% $m->notes("title") || "My Application" %></title> | ||
| 101 | </head> | ||
| 102 | |||
| 103 | |||
| 104 | =head1 SEE ALSO | ||
| 105 | |||
| 106 | L<MasonX::MiniMVC> | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 1 | 1; |