#!/usr/bin/env perl use Mojolicious::Lite; get '/' => "about"; my @menu = ( nav => [ qw{overview examples source feedback} ], sidebar => { overview => [ qw{toto/elements element toto/quickstart toto/inspiration toto/examples toto/download} ], examples => [ qw{example/list example}, ], source => [ qw{file/list file}, ], feedback => [ qw{comments/view comments/add}, ], }, tabs => { element => [ qw/description example/ ], example => [ qw/description source/ ], file => [ qw/pod raw git/ ], } ); plugin toto => @menu; app->start; __DATA__ @@ about.html.ep

Toto

A navigational structure based on tabs and objects.


<%= link_to "https://metacpan.org/module/Mojolicious::Plugin::Toto" => begin %>Mojolicious-Plugin-Toto<%= end %> uses <%= link_to "http://mojolicio.us" => begin %>Mojolicious<%= end %> and twitter bootstrap's <%= link_to "http://twitter.github.com/bootstrap/examples/fluid.html" => begin %>fluid layout example<%= end %> to create a navigational structure and set of routes for a web application.

<%= link_to "toto/elements", class =>"btn btn-primary btn-large" => begin %> Learn more <%= end %>

@@ toto/elements.html.ep

Each page in an application created with toto has :

Pages are created using <%= link_to "element/default" => { key => "template" } => begin %> templates<%= end %>.

The toto <%= link_to "element/default" => { key => "layout" } => begin %> layout<%= end %> generates the navigational structure.

A number of <%= link_to "element/default" => { key => "helpers" } => begin %> helpers <%= end %> are added for use within templates.

A number of <%= link_to "element/default" => { key => "stashkey" } => begin %> stash keys %= end are set which may also be accessed from within the templates.

@@ toto/quickstart.html.ep

To get a sample toto site running, just <%= link_to "toto/download" => begin %>download<%= end %> toto, and run one of the <%= link_to "toto/examples" => begin %>examples<%= end %>.

$ cpanm Mojolicious::Plugin::Toto
$ cpanm --look Mojolicious::Plugin::Toto
$ ./eg/toto.pl daemon
@@ toto/download.html.ep

<%= link_to "https://metacpan.org/module/Mojolicious::Plugin::Toto" => begin %>Mojolicious-Plugin-Toto<%= end %> is available on <%= link_to "http://cpan.org" => begin %>CPAN<%= end %>.

It can be downloaded from there directly, or using a tool, such as <%= link_to "https://metacpan.org/module/cpanm" => begin %>cpanm<%= end %>.

curl http://cpanmin.us > ~/bin/cpanm
chmod +x ~/bin/cpanm
~/bin/cpanm Mojolicious::Plugin::Toto
@@ element/navbar/description.html.ep The nav bar is created from a list of words. The words are used for a few purposes :
  1. as labels in the tool bar at the top of every page.
  2. as URLs for which a 302 redirect will be generated, sending the user to the first item in the corresponding <%= link_to 'element/default', { key => 'sidebar' } => begin %> sidebar<%= end %>.
@@ element/sidebar/description.html.ep

The side bar is a hash mapping nav bar items to lists of elements in the side bar.

Keys in the hash are elements of the nav bar array. Values in the hash may take one of two forms :

  1. foo/bar : means that the route will be handled by controller foo, and action bar.
  2. baz : means that the route will generate a row of <%= link_to 'element/default', { key => 'tabs' } => begin %> tabs<%= end %>. The tabs all act on a baz object.
@@ element/tabs/description.html.ep Tabs are specified as a hash whose keys are the type of object and whose values are lists of words. The words are used as
  1. labels for the tabs
  2. portions of the URL paths (the paths are of the form object/action/instance)
  3. names of the routes.
@@ element/example.html.ep Sample <%= current_instance->key %> configuration : % my $what = current_instance->key; % $what = 'nav' if $what eq 'navbar';
%= dumper( toto_config->{$what} )
@@ element/template/description.html.ep

Templates in a toto application may be found in several locations, depending on how generic the template is. templates from the following paths are loaded :

  1. class/object/instance.html.ep (if applicable)
  2. class/object.html
  3. object.html
@@ element/template/example.html.ep For example, the code for this template is :
%= $self->app->renderer->get_data_template({},'element/template/example.html.ep');
but the code for the more generic "example" template, used by most of the other example pages on this site ( <%= link_to 'element/example', { key => 'navbar' } => begin %>navbar<%= end %>, <%= link_to 'element/example', { key => 'sidebar' } => begin %>sidebar<%= end %>, etc. ) is :
%= $self->app->renderer->get_data_template({},'element/example.html.ep');
@@ element/object/description.html.ep

Toto makes use of the objects in your application, by using the namespace you provide when you initilize the plugin. When a route of the form "$noun/$key/$action" is encountered, $noun will be camelcased and appended to the namespace. Then a constructor will be called which uses $key, like so :

$c->model_class->new(key => $key);

This behavior is encapsulated in a helper called "current_instance". To change this default behavior, write another "current_instance" helper to replace the default one.

Besides new(), the following methods may be implemented :

autocomplete
Receives the named parameters: q, object, c and tab. Returns an array of names and hrefs to be used on an autocomplete dropdown.
stringification
Overload stringification to change the way instances are displayed at the top of pages.
@@ element/object/example.html.ep The default object class is Mojolicious::Plugin::Toto::Model.
%= Mojo::Asset::File->new(path => $INC{q[Mojolicious/Plugin/Toto/Model.pm]})->slurp;
@@ element/helpers/description.html.ep
toto_config
The configuration (defined when the plugin is loaded).
model_class
The default model class.
tabs
The list of current tabs being displayed.
current_object
The current object's class.
current_tab
The current tab.
current_instance
The current instance.
@@ element/helpers/example.html.ep
% for (qw/toto_config model_class tabs current_object current_tab current_instance/) {
<%= $_ %>
<%= dumper(eval "$_()") %>
% }
@@ element/stashkey/description.html.ep A number of keys are set in the stash for use within templates :
$object
The current object type.
$noun
Synonym for $object.
$tab
The current tab.
$nav_item
The current item in the top nav bar.
$$noun
The variable whose name is "$noun" is a synonym for <%= link_to 'element/description', { key => 'helpers' } => begin %> current_instance. %= end
@@ element/stashkey/example.html.ep
% for (qw[object tab nav_item noun]) {
$<%= $_ %>
<%= eval '$'.$_; %>
% }
$<%= $noun %> (==${$noun})
<%= eval '$'.$noun %>
@@ element/layout/description.html.ep Toto sets the default layout to be "toto", e.g.
  $app->defaults(layout => "toto")
The layout and supporting files, are builded with the plugin and used as defaults, but may be overridden. Some stash values may be set for minimal customization : (or of course another layout can used in place of the provided one). @@ element/layout/example.html.ep Below is the default layout (layouts/toto.html.ep).
% my $path = $self->app->renderer->template_path({template => "layouts/toto", format => "html", handler => 'ep'});
%= Mojo::Asset::File->new(path => $path)->slurp;