Mojolicious::Plugin::Charset - Charset Plugin


Mojolicious documentation Contained in the Mojolicious distribution.

Index


Code Index:

NAME

Top

Mojolicious::Plugin::Charset - Charset Plugin

SYNOPSIS

Top

  # Mojolicious
  $self->plugin(charset => {charset => 'Shift_JIS'});

  # Mojolicious::Lite
  plugin charset => {charset => 'Shift_JIS'};

DESCRIPTION

Top

Mojolicious::Plugin::Charset is a plugin to easily set the default charset and encoding on all layers of Mojolicious.

OPTIONS

Top

charset

  # Mojolicious::Lite
  plugin charset => {charset => 'Shift_JIS'};

Application charset.

METHODS

Top

Mojolicious::Plugin::Charset inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

  $plugin->register;

Register plugin hooks in Mojolicious application.

SEE ALSO

Top

Mojolicious, Mojolicious::Guides, http://mojolicio.us.


Mojolicious documentation Contained in the Mojolicious distribution.

package Mojolicious::Plugin::Charset;
use Mojo::Base 'Mojolicious::Plugin';

# "Shut up friends. My internet browser heard us saying the word Fry and it
#  found a movie about Philip J. Fry for us.
#  It also opened my calendar to Friday and ordered me some french fries."
sub register {
  my ($self, $app, $conf) = @_;

  # Got a charset
  $conf ||= {};
  if (my $charset = $conf->{charset}) {

    # Add charset to text/html content type
    $app->types->type(html => "text/html;charset=$charset");

    # Allow defined but blank encoding to suppress unwanted
    # conversion
    my $encoding =
      defined $conf->{encoding}
      ? $conf->{encoding}
      : $conf->{charset};
    $app->renderer->encoding($encoding) if $encoding;

    # This has to be done before params are cloned
    $app->hook(after_build_tx => sub { shift->req->default_charset($charset) }
    );
  }
}

1;
__END__