Now we need some way of connecting which view to show for any given action. A View::Map object controls this.
One could just return a view from the controller if they wanted:
sub do_action {
my($self,$req) = @_;
return(My::Custom::View->new());
}
This however poses a problem, what if you wanted to provide for multiple output formats? For example, ajax as well as regular html and plain-text?
We need some way of controlling the output, some way of changing the view depending on what the client wants. (Remember WAP ? it's the same basic idea)
Another problem with template oriented views (or views that are not mapped in general) is that of code re-use, what if you have the same screen that you want to re-use in several places, but you want to keep the door open for future changes?
A re-usable screen (in imaginary text) might look like this:
<h1>Error: <%= $error_title; %></h1>
<p><%= $error_message; %></p>
You might then re-use this same dialog box in multiple actions. Some place down the road, you may need to change one of them for a special case.
<h1>Error: <%= $error_title; %></h1>
<p><%= $error_message; %></p>
<p>This can happen if you haven't checked the FOO button</p>
If you just returned the same view, directly from the controller method, you would have a problem, you would have "tied the view to the model".
What if you wanted to change the view based on the context of the request? This is why there are multiple viewmap sections in the configuration, a default and then others for special circumstances.
The MapName parameter of ViewMap tells the particular
viewmap object which section to use.
Since the ViewMap is tied to the request and not the controller, you can change it (for example, depending on a cookie, URL, browser info, etc..)
An important aspect of this is that the controller should not care which underlying viewmap is in use, it simply calls:
$view = $req->get_view('ok');
# or
$view = $req->get_view('error');
Both ok and error are mapped to view packages and options for that specific view, totally isolated from the controller. For all intents and purposes, the request might as well be an image, ajax or even a "gopher" page.