From d5a31b1c95ddb332952b5c55970ddec335697272 Mon Sep 17 00:00:00 2001 From: Mathias Kende Date: Mon, 10 May 2021 23:57:09 +0200 Subject: [PATCH 1/2] Add a template_path option to Mojolicious::Renderer::render that allow to pass the path to a specific template file. --- lib/Mojolicious/Guides/Rendering.pod | 5 +++++ lib/Mojolicious/Renderer.pm | 13 +++++++++---- t/mojolicious/layouted_lite_app.t | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/Mojolicious/Guides/Rendering.pod b/lib/Mojolicious/Guides/Rendering.pod index 4e0a919bb0..e1e0362299 100644 --- a/lib/Mojolicious/Guides/Rendering.pod +++ b/lib/Mojolicious/Guides/Rendering.pod @@ -198,6 +198,11 @@ L to try multiple alternatives. $c->render_maybe('localized/baz') or $c->render('foo/bar/baz'); +If you want to render a template from a specific file not in a C directory, you can pass it with the +C option. + + $c->render(template_path => '/path/to/the/template'); + =head2 Rendering to strings Sometimes you might want to use the rendered result directly instead of generating a response, for example, to send diff --git a/lib/Mojolicious/Renderer.pm b/lib/Mojolicious/Renderer.pm index 1e7dd70d1f..ae8b6ecaaf 100644 --- a/lib/Mojolicious/Renderer.pm +++ b/lib/Mojolicious/Renderer.pm @@ -86,6 +86,7 @@ sub render { encoding => $self->encoding, handler => $stash->{handler}, template => delete $stash->{template}, + template_path => delete $stash->{template_path}, variant => $stash->{variant} }; my $inline = $options->{inline} = delete $stash->{inline}; @@ -102,12 +103,13 @@ sub render { return encode_json(delete $stash->{json}), 'json' if exists $stash->{json}; # Template or templateless handler - $options->{template} //= $self->template_for($c); + $options->{template} //= $self->template_for($c) unless $options->{template_path}; return () unless $self->_render_template($c, \my $output, $options); - + # Inheritance my $content = $stash->{'mojo.content'} //= {}; local $content->{content} = $output =~ /\S/ ? $output : undef if $stash->{extends} || $stash->{layout}; + delete $options->{template_path}; while ((my $next = _next($stash)) && !defined $inline) { @$options{qw(handler template)} = ($stash->{handler}, $next); $options->{format} = $stash->{format} || $self->default_format; @@ -162,6 +164,7 @@ sub template_handler { sub template_name { my ($self, $options) = @_; + return $options->{template_path} if $options->{template_path}; return undef unless defined(my $template = $options->{template}); return undef unless my $format = $options->{format}; $template .= ".$format"; @@ -181,6 +184,7 @@ sub template_name { sub template_path { my ($self, $options) = @_; + return $options->{template_path} if $options->{template_path}; return undef unless my $name = $self->template_name($options); my @parts = split /\//, $name; -r and return $_ for map { path($_, @parts)->to_string } @{$self->paths}, $TEMPLATES; @@ -434,7 +438,7 @@ handler could be found. }); Return a template name for an options hash reference with C