package HomePanel::Render; use Moo; use Types::Path::Tiny 'AbsFile'; use Template::Provider::Encoding; use Template::Stash::ForceUTF8; use Template; use DateTime; use DateTime::Format::Duration; use namespace::clean; has [qw(provider stash template)] => ( is => 'lazy', ); sub _build_provider { Template::Provider::Encoding->new( ABSOLUTE => 1, RELATIVE => 1, ); } sub _build_stash { Template::Stash::ForceUTF8->new; } sub _build_template { my ($self) = @_; Template->new( LOAD_TEMPLATES => [ $self->provider ], STASH => $self->stash ); } has template_file => ( is => 'ro', isa => AbsFile, coerce => AbsFile->coercion, required => 1, ); { my %icon_for=( 'clear-day' => 'sun-day', 'clear-night' => 'sun-night', rain => 'rain', snow => 'snow', sleet => 'sleet', wind => 'wind', fog => 'fog', cloudy => 'cloudy', 'partly-cloudy-day' => 'cloud-day', 'partly-cloudy-night' => 'cloud-night', ); sub icon_for { my ($status) = @_; return "icons/".($icon_for{$status}//'45').".png"; } } sub render { my ($self,$data) = @_; my $output; $self->template->process( $self->template_file->stringify, { f => $data->{forecast}, b => $data->{bus}, t => $data->{tube}, icon_for => \&icon_for, now => DateTime->now, format_duration => sub { DateTime::Format::Duration->new(@_) }, }, \$output, ) or die $self->template->error; return $output; }