--- /dev/null
+package Newslash::Plugin::RendererHelper;
+use Mojo::Base 'Mojolicious::Plugin';
+use Time::HiRes qw(time);
+
+sub register {
+ my ($self, $app, $conf) = @_;
+ $self->{app} = $app;
+ $self->{conf} = $conf;
+ $self->{functions} = {};
+
+ $app->helper(renderer_helper => sub { state $renderer_helper = $self; });
+}
+
+sub add_function {
+ my ($self, $namespace, $name, $function) = @_;
+
+ $self->{functions} ||= {};
+ $self->{functions}->{$namespace} ||= {};
+
+ $self->{functions}->{$namespace}->{$name} = $function;
+}
+
+sub get_functions {
+ my ($self, $namespace) = @_;
+ return if !$self->{functions};
+ return $self->{functions}->{$namespace} || {};
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Plugin::RendererHelper - Helper functions for Renderer
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $app->plugin('Newslash::Plugin::RendererHelper');
+
+=head1 DESCRIPTION
+
+L<Newslash::Plugin::RendererHelper> porvides some function to help renderer.
+
+
+=head1 METHODS
+
+=head2 register
+
+ $plugin->register(Mojolicious->new);
+
+Register epoch in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
+
+=cut
package Newslash::Plugin::TT2Renderer::Provider;
use base qw(Template::Provider);
- my $EXT = ["", ".html.tt2", ".tt2"];
+ my $EXT = ["", ".js.tt2", ".html.tt2", ".tt2"];
sub _find_template {
my ($self, $fname) = @_;
return $self->{tt}->process($template, $vars, $output_ref, $options);
}
+sub render_content {
+ my ($self, $template, $vars, $output_ref, $paths, $options) = @_;
+ $options ||= {};
+ $options->{INCLUDE_PATH} = $paths;
+ $self->{tt_provider}->include_path($paths);
+
+ return $self->{tt}->process($template, $vars, $output_ref, $options);
+}
+
sub register {
my ($self, $app, $conf) = @_;
};
$self->define_vmethod('list', 'rand', $rand_vmethod);
- $app->renderer->add_handler( 'tt2' => sub {
+ $app->renderer->add_handler('tt2' => sub {
my ($renderer, $c, $output, $options) = @_;
my $inline_template = $options->{inline};
$vars->{Newslash} = {};
$vars->{Newslash}->{config} = $c->app->config;
+ if ($app->renderer_helper) {
+ my $funcs = $app->renderer_helper->get_functions('Newslash');
+ for my $name (keys %$funcs) {
+ $vars->{Newslash}->{$name} = $funcs->{$name};
+ }
+ }
+
$vars->{NS} = {};
for my $k (keys %{$self->{NS_plugin}}) {
if (ref($self->{NS_plugin}->{$k}) eq 'CODE') {
# when "test" mode, output debug logs.
$app->log->level('debug') if $app->mode eq 'test';
+ # renderer helper
+ $app->plugin('Newslash::Plugin::RendererHelper');
+
# enable logging
$app->plugin('Newslash::Plugin::AccessLog::Debug', $app->config->{Log} || {});
$app->plugin('Newslash::Plugin::AccessLog::LocalFile', $app->config->{Log} || {});