The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::Toto - A simple tab and object based site structure

SYNOPSIS

    #!/usr/bin/env perl

    use Mojolicious::Lite;

    plugin 'toto' =>
            nav => [ qw{brewery pub beer} ],
            sidebar => {
                brewery => [ qw{brewery/list brewery/search brewery} ],
                pub     => [ qw{pub/list pub/search pub} ],
                beer    => [ qw{beer/list beer/search beer} ],
            },
            tabs => {
                brewery => [qw/view edit delete/],
                pub     => [qw/view edit delete/],
                beer    => [qw/view edit delete/],
            };

    app->start;

DESCRIPTION

This plugin provides a navigational structure and a default set of routes for a Mojolicious or Mojolicious::Lite app

The navigational structure is a slight variation of this example used by twitter's bootstrap.

The plugin provides a sidebar, a nav bar, and also a row of tabs underneath the name of an object.

The row of tabs is an extension of BREAD or CRUD -- in a BREAD application, browse and add are operations on zero or many objects, while edit, add, and delete are operations on one object. In the toto structure, these two types of operations are distinguished by placing the former in the side nav bar, and the latter in a row of tabs underneath the object to which the action applies.

Additionally, a top nav bar contains menu items to take the user to a particular side bar.

HOW DOES IT WORK

After loading the toto plugin, the default layout is set to 'toto'.

Defaults routes are generated for every sidebar entry and tab entry.

The names of the routes are of the form "controller/action", where controller is both the controller class and the model class.

The following templates will be automagically used, if found (in order of preference) :

  - templates/<controller>/<instance>/<action>.html.ep
  - templates/<controller>/<action>.html.ep
  - templates/<action>.html.ep

Or if no object is selected :

  - templates/<controller>/none_selected.html.ep

(This one links connects to "list" and "search" if these routes exist, and provides an autocomplete form if the model class has an autocomplete() method.)

Also the templates "single" and "plural" are built-in fallbacks for the two cases described above.

The stash values "object" and "tab" are set for each auto-generated route. Also "noun" is set as an alias to "object".

A version of twitter's bootstrap is included in this distribution.

OPTIONS

In addition to "menu", "nav/sidebar/tabs", the following options are recognized :

prefix
    prefix => /my/subpath

A prefix to prepend to the path for the toto routes.

head_route
    head_route => $app->routes->find('top_route");

A Mojolicious::Route::Route object to use as the parent for all routes.

model_namespace
    model_namespace => "Myapp::Model'

A namespace for model classes : the model class will be camelized and appended to this.

EXAMPLE

There are two different structures that toto will accept. One is intended for a simple CRUD structure, where each object has its own top level navigational item and a variety of possible actions. The other form is intended for a more complex situation in which the list of objects does not correspond to the list of choices in the navigation bar.

Simple structure

The "menu" format can be used to automatically generate the nav bar, side bar and rows of tabs, using actions which correspond to many objects or actions which correspond to one object.

    #!/usr/bin/env perl

    use Mojolicious::Lite;

    plugin 'toto' =>
         menu => [
            beer => {
                many => [qw/search browse/],
                one  => [qw/picture ingredients pubs/],
            },
            pub => {
                many => [qw/map list search/],
                one  => [qw/info comments/],
            }
        ];

    app->start;

Complex structure

The "nav/sidebar/tabs" format can be used for a more versatile structure, in which the nav bar and side bar are less constrained.

     use Mojolicious::Lite;

     get '/my/url/to/list/beers' => sub {
          shift->render_text("Here is a page for listing beers.");
     } => "beer/list";

     get '/beer/create' => sub {
        shift->render_text("Here is a page to create a beer.");
     } => "beer/create";

     plugin 'toto' =>

          # top nav bar items
          nav => [
              'brewpub',          # Refers to a sidebar entry below
              'beverage'          # Refers to a sidebar entry below
          ],

          # possible sidebars, keyed on nav entries
          sidebar => {
            brewpub => [
                'brewery/phonelist',
                'brewery/mailing_list',
                'pub/search',
                'pub/map',
                'brewery',        # Refers to a "tab" entry below
                'pub',            # Refers to a "tab" entry below
            ],
            beverage =>
              [ 'beer/list',      # This will use the route defined above named "beer/list"
                'beer/create',
                'beer/search',
                'beer/browse',    # This will use the controller at the top (Beer::browse)
                'beer'            # Refers to a "tab" entry below
               ],
          },

          # possible rows of tabs, keyed on sidebar entries without a /
          tabs => {
            brewery => [ 'view', 'edit', 'directions', 'beers', 'info' ],
            pub     => [ 'view', 'info', 'comments', 'hours' ],
            beer    => [ 'view', 'edit', 'pictures', 'notes' ],
          };
     ;

     app->start;

NOTES

To create pages outside of the toto framework, just set the layout to something other than "toto', e.g.

    get '/no/toto' => { layout => 'default' } => ...

This module is experimental. The API may change without notice. Feedback is welcome!

TODO

Document the autcomplete API.

AUTHOR

Brian Duggan bduggan@matatu.org

SEE ALSO

beer.dotcloud.com