Skip to content

Commit 8df430a

Browse files
committed
For [GH #159] - $YATT->render() - add support for deep:widget:path
1 parent 510ef16 commit 8df430a

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Lite.pm

+24-3
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,25 @@ sub render_encoded {
226226
}
227227

228228
sub render_into {
229-
local ($YATT, $CON) = splice @_, 0, 2;
230-
$YATT->open_trans->render_into($CON, @_);
231-
try_invoke($CON, 'flush_headers');
229+
(my MY $self, my ($con, $file, $args)) = @_;
230+
local ($YATT, $CON) = ($self, $con);
231+
if (ref $file eq 'ARRAY') {
232+
# [$file, $type, $item]
233+
$self->open_trans->render_into($CON, $file, $args);
234+
} else {
235+
$self->raw_render_into(
236+
$con,
237+
YATT::Lite::Util::rootname($file || $self->{cf_index_name}),
238+
$args,
239+
);
240+
}
241+
try_invoke($con, 'flush_headers');
242+
}
243+
244+
sub raw_render_into {
245+
(local ($YATT, $CON), my ($file, $args)) = @_;
246+
my ($part, $sub, $pkg) = $YATT->open_trans->find_part_renderer($file);
247+
$sub->($pkg, $CON, $part->reorder_hash_params($args));
232248
}
233249

234250
sub find_handler {
@@ -687,6 +703,11 @@ sub default_lang {
687703

688704
#========================================
689705
# Delegation to the core(Translator, which is useless for non-templating.)
706+
#
707+
# Note: these wrappers do not call YATT::Lite::VFS->reset_refresh_mark.
708+
# This means once templates is compiled, it is not updated even if
709+
# they are modified. You can avoid this behavior by
710+
# directly using $YATT->open_trans->xxx instead.
690711
#========================================
691712
foreach
692713
(qw/find_part

Lite/Core.pm

+29
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ use YATT::Lite::Breakpoint ();
151151

152152
sub YATT::Lite::Core::Part::reorder_hash_params {
153153
(my Widget $widget, my ($orig_params)) = @_;
154+
return unless $orig_params;
155+
return @$orig_params if ref $orig_params eq 'ARRAY';
154156
my $params = +{%$orig_params};
155157
my @params;
156158
foreach my $name (map($_ ? @$_ : (), $widget->{arg_order})) {
@@ -356,6 +358,33 @@ sub synerror {
356358
wantarray ? ($part, $tmpl) : $part;
357359
}
358360

361+
sub find_part_renderer {
362+
(my MY $self, my ($widgetPath, %opts)) = @_;
363+
my $ignore_error = delete $opts{ignore_error};
364+
365+
my @wpath = ref $widgetPath ? @$widgetPath : split ":", $widgetPath;
366+
367+
my $part = $self->find_part_from($self->{root}, @wpath ? @wpath : '')
368+
or ($ignore_error and return)
369+
or croak "No such widget: ".join(":", @wpath);
370+
371+
my $tmpl = $part->cget('folder');
372+
373+
my $path = $tmpl->cget('path');
374+
375+
my $method = "render_".$part->cget('name');
376+
377+
my $pkg = $self->find_product(perl => $tmpl)
378+
or ($ignore_error and return)
379+
or croak "Can't compile template file: $path";
380+
381+
my $sub = $pkg->can($method)
382+
or ($ignore_error and return)
383+
or croak "Can't extract $method from file: $path";
384+
385+
($part, $sub, $pkg);
386+
}
387+
359388
sub find_part_handler {
360389
(my MY $self, my $nameSpec, my %opts) = @_;
361390
my $ignore_error = delete $opts{ignore_error};

0 commit comments

Comments
 (0)