Mojolicious::Plugin::DefaultHelpers - Default helpers plugin
# Mojolicious $self->plugin('DefaultHelpers'); # Mojolicious::Lite plugin 'DefaultHelpers';
Mojolicious::Plugin::DefaultHelpers is a collection of renderer helpers for Mojolicious.
This is a core plugin, that means it is always enabled and its code a good example for learning to build new plugins, you're welcome to fork it.
See "PLUGINS" in Mojolicious::Plugins for a list of plugins that are available by default.
Mojolicious::Plugin::DefaultHelpers implements the following helpers.
%= accepts->[0] // 'html' %= accepts('html', 'json', 'txt') // 'html'
Select best possible representation for resource from Accept request header, format stash value or format GET/POST parameter with "accepts" in Mojolicious::Renderer, defaults to returning the first extension if no preference could be detected.
Accept
format
GET
POST
# Check if JSON is acceptable $self->render(json => {hello => 'world'}) if $self->accepts('json'); # Check if JSON was specifically requested $self->render(json => {hello => 'world'}) if $self->accepts('', 'json'); # Unsupported representation $self->render(data => '', status => 204) unless my $format = $self->accepts('html', 'json'); # Detected representations to select from my @formats = @{$self->accepts};
%= app->secrets->[0]
Alias for "app" in Mojolicious::Controller.
%= b('test 123')->b64_encode
Turn string into a Mojo::ByteStream object.
%= c(qw(a b c))->shuffle->join
Turn list into a Mojo::Collection object.
%= config 'something'
Alias for "config" in Mojo.
%= content foo => begin test % end %= content bar => 'Hello World!' %= content 'foo' %= content 'bar' %= content
Store partial rendered content in named buffer and retrieve it, defaults to retrieving the named buffer content, which is commonly used for the renderers layout and extends features. Note that new content will be ignored if the named buffer is already in use.
content
layout
extends
% content_for foo => begin test % end %= content_for 'foo'
Append partial rendered content to named buffer and retrieve it. Note that named buffers are shared with the "content" helper.
% content_for message => begin Hello % end % content_for message => begin world! % end %= content_for 'message'
%= csrf_token
Get CSRF token from "session", and generate one if none exists.
% if (current_route 'login') { Welcome to Mojolicious! % } %= current_route
Check or get name of current route.
%= dumper {some => 'data'}
Dump a Perl data structure with "dumper" in Mojo::Util.
% extends 'blue'; % extends 'blue', title => 'Blue!';
Extend a template. All additional values get merged into the "stash".
%= flash 'foo'
Alias for "flash" in Mojolicious::Controller.
%= include 'menubar' %= include 'menubar', format => 'txt'
Alias for Mojolicious::Controller/"render_to_string".
Mojolicious::Controller/"render_to_string"
% layout 'green'; % layout 'green', title => 'Green!';
Render this template with a layout. All additional values get merged into the "stash".
%= param 'foo'
Alias for "param" in Mojolicious::Controller.
%= session 'foo'
Alias for "session" in Mojolicious::Controller.
%= stash 'foo' % stash foo => 'bar';
Alias for "stash" in Mojolicious::Controller.
%= stash('name') // 'Somebody'
% title 'Welcome!'; % title 'Welcome!', foo => 'bar'; %= title
Page title. All additional values get merged into the "stash".
%= ua->get('mojolicio.us')->res->dom->at('title')->text
Alias for "ua" in Mojo.
%= url_for 'named', controller => 'bar', action => 'baz'
Alias for "url_for" in Mojolicious::Controller.
%= url_with 'named', controller => 'bar', action => 'baz'
Does the same as "url_for", but inherits query parameters from the current request.
%= url_with->query([page => 2])
%= validation->param('foo')
Alias for "validation" in Mojolicious::Controller.
Mojolicious::Plugin::DefaultHelpers inherits all methods from Mojolicious::Plugin and implements the following new ones.
$plugin->register(Mojolicious->new);
Register helpers in Mojolicious application.
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.