Add a helper for formatting numbers.

This commit is contained in:
barnold 2022-09-18 18:56:42 +01:00
parent c9c248a4b3
commit 74ae6e1745
4 changed files with 19 additions and 3 deletions

View File

@ -1,12 +1,27 @@
package MyApp;
use Mojo::Base 'Mojolicious', -signatures;
# Define home-made helpers.
sub add_my_helpers ($self) {
$self->helper(
commify => sub ($self, $number) {
my $rev = reverse $number;
while ($rev =~ s/(\d{3})(\d)/$1,$2/) {};
return scalar reverse $rev;
}
);
return;
}
sub startup ($self) {
# Set a "secret" for signing cookies, preventing "Your secret
# passphrase needs to be changed" in the log, using my_app.yml.
my $config = $self->plugin('NotYAMLConfig');
$self->secrets([ $config->{secrets} ]);
# Add home-made helper(s).
$self->add_my_helpers;
# Set routes.
my $r = $self->routes;
# Home gets an explicit name, else its name is an empty string. A

View File

@ -1,5 +1,6 @@
<p/>
Page <%= $pager->current_page %> of <%= $pager->last_page %>
Page <%= commify($pager->current_page) %>
of <%= commify($pager->last_page) %>
&nbsp;&nbsp;&nbsp;
<%= link_to url_with(page_number => 1) => begin %>◄ First<% end %>

View File

@ -10,7 +10,7 @@
%= end
<p/>
<%= $pager->total_entries %> authors found.
<%= commify($pager->total_entries) %> authors found.
<p/>
<div>

View File

@ -10,7 +10,7 @@
%= end
<p/>
<%= $pager->total_entries %> books found.
<%= commify($pager->total_entries) %> books found.
<p/>
<div>