Mojolicious::Plugin::DomIdHelper - Mojolicious plugin to generate DOM IDs and CSS class names from your ORM objects
# Mojolicious $self->plugin('dom_id_helper'); # Or, your defaults $self->plugin('dom_id_helper', delimiter => '-') # Mojolicious::Lite plugin 'dom_id_helper'; # Set defaults plugin 'dom_id_helper', delimiter => '-' # Your view <div id="<%= dom_id($object) %>" class="<%= dom_class($object) %>"> ... </div> <div id="<%= dom_id($object, method => 'name') ) %>"> ... </div>
DOM IDs are generated by joining an object's package name and its primary key with the character specified by the "delimiter" option. By default the primary key is retrieved via a method named id. This can be modified, see "OPTIONS".
id
By default, given an instance of DB::Package::User with an ID of 1:
DB::Package::User
1
dom_id($user) dom_class($user)
will generate:
user_1 user
For dom_id, if the primary key is undefined only the package name is returned. If $user is not a reference undef is returned.
dom_id
$user
undef
Multi-column primary keys are not separated by the "delimiter" option, they are concatenated.
For references that aren't blessed dom_id and dom_class return the reference type. If Lingua::EN::Inflect is installed array references that contain a blessed reference will return the pluralized form of the blessed reference.
dom_class
For example, if Lingua::EN::Inflect is installed:
dom_id([$user]) dom_class([$user]) dom_id([]) dom_id({ user => $user }) dom_id({})
users users array hash hash
If Lingua::EN::Inflect is not installed dom_id([$user]) will return array.
dom_id([$user])
array
The aim is to be ORM agnostic. Just set the "method" option to the name of the method used to retrieve your object's primary key.
Multi-column primary keys returned as array references will cause problems (for now).
delimiter
plugin 'dom_id_helper', delimiter => '-'
The character used to delimit the object's package name from its primary key. Defaults to '_'.
'_'
method
plugin 'dom_id_helper', method => 'name' plugin 'dom_id_helper', method => [qw{first_name last_name}]
The method used to retrieve the object's primary key. Defaults to 'id'.
'id'
keep_namespace
plugin 'dom_id_helper', keep_namespace => 1
Keep the full package name when generating the DOM ID. Defaults to 0 (false).
0
Skye Shaw <sshaw AT lucas.cis.temple.edu>
Mojolicious and Mojolicious::Plugin::TagHelpers
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Mojolicious::Plugin::DomIdHelper, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::DomIdHelper
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::DomIdHelper
For more information on module installation, please visit the detailed CPAN module installation guide.