MooseX::MojoControllerExposingAttributes - Expose controller attributes to Mojolicious
version 1.000001
package MyApp::Controller::Example; use MooseX::MojoControllerExposingAttributes; ...; has some_attribute => ( is => 'ro', traits => ['ExposeMojo'], ); # then later in a template: <%= ctrl->some_attribute %>
This module is for advanced use. $c/$self are already made available in templates and are likely sufficient for the majority of use cases. This module was created in order to expose Moose attributes in a way where you don't have to stash them every single time you want to use them.
$c
$self
This class allows you to expose selected Moose attributes from your Mojolicious controller to your templates by marking them with the ExposeMojo trait.
ExposeMojo
Using this class in a Perl class does several things:
So rather than declaring your controller class a Moose Mojolicious Controller in the usual way:
package MyApp::Controller::Example; use Mojo::Base 'Mojolicious::Controller'; use Moose::NonMoose; use Moose;
You should simply say:
package MyApp::Controller::Example; use MooseX::MojoControllerExposingAttributes;
Once you've done that then you can define attributes in the class (or in roles the class consumes) that are exposed to Mojolicious.
has some_attribute => ( is => 'ro', traits => ['ExposeMojo'], ); has some_attribute_with_a_really_long_name => ( is => 'ro', traits => ['ExposeMojo'], expose_to_mojo_as => 'shorter_name', );
In order to get the ctrl helper you should make sure you've loaded the Mojolicious::Plugin::ExposeControllerMethod plugin somewhere in your Mojolicious application, typically within the startup method itself:
ctrl
startup
sub startup { my $self = shift; $self->plugin('ExposeControllerMethod'); ... }
Then you'll be able to access your attributes from within templates that are rendered from that controller:
some attribute: <%= ctrl->some_attribute %> some attribute with a really long name: <%= ctrl->shorter_name %>
It would be nice to be able to set the baseclass instead of always using Mojolicious::Controller
Mojolicious::Plugin::ExposeControllerMethod
MooseX::MojoControllerExposingAttributes::Trait::Attribute
Mark Fowler <mfowler@maxmind.com>
Dave Rolsky <drolsky@maxmind.com>
Olaf Alders <oalders@maxmind.com>
This software is copyright (c) 2015 - 2016 by MaxMind, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MooseX::MojoControllerExposingAttributes, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::MojoControllerExposingAttributes
CPAN shell
perl -MCPAN -e shell install MooseX::MojoControllerExposingAttributes
For more information on module installation, please visit the detailed CPAN module installation guide.