Committer: ssafronova
LJSUP-6706: Sign up server side - LJSUP-6843: prepare templated widget base classU trunk/cgi-bin/LJ/Widget/ExampleAjaxWidget.pm A trunk/cgi-bin/LJ/Widget/Template.pm
Modified: trunk/cgi-bin/LJ/Widget/ExampleAjaxWidget.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/ExampleAjaxWidget.pm 2010-09-21 08:30:13 UTC (rev 17423) +++ trunk/cgi-bin/LJ/Widget/ExampleAjaxWidget.pm 2010-09-21 08:32:24 UTC (rev 17424) @@ -15,7 +15,8 @@ my $submitted = $opts{submitted} ? 1 : 0; $ret .= "This widget does an AJAX POST.<br />"; - $ret .= "Render it with: <code>LJ::Widget::ExampleAjaxWidget->render;</code>"; + $ret .= 'Render it with: <code><br>my $widget = LJ::Widget::ExampleAjaxWidget->new;'; + $ret .= '<br>$headextra .= $widget->wrapped_js;<br>$body .= $widget->render();</code>'; $ret .= $class->start_form( id => "ajax_form" ); $ret .= "<p>Type in a word: " . $class->html_text( name => "text", size => 10 ) . " "; $ret .= $class->html_submit( button => "Click me!" ) . "</p>"; Added: trunk/cgi-bin/LJ/Widget/Template.pm =================================================================== --- trunk/cgi-bin/LJ/Widget/Template.pm (rev 0) +++ trunk/cgi-bin/LJ/Widget/Template.pm 2010-09-21 08:32:24 UTC (rev 17424) @@ -0,0 +1,52 @@ +package LJ::Widget::Template; +# base class for widget of any time (post / ajax / simple(render) ), using template engine + +use strict; +use base qw(LJ::Widget); +use Carp qw(croak); + +# main 'templated' method to override in subclass +sub prepare_template_params { + my $class = shift; + my $template_obj = shift; + my $opts = shift; + + return; +} + +# path to template file, +# subclass may skip overriding this method +sub template_filename { + my $class = shift; + + my $lc_class = lc $class->subclass; + $lc_class =~ s|::|/|g; + return "$ENV{'LJHOME'}/templates/Widgets/${lc_class}.tmpl"; +} + +# fully ready 'render_body' method, subclass have no need to override this method +sub render_body { + my $class = shift; + my %opts = @_; + + my $filename = $class->template_filename; + my $template = LJ::HTML::Template->new( + { use_expr => 1 }, # force HTML::Template::Pro with Expr support + filename => $filename, + die_on_bad_params => 0, + strict => 0, + ) or die "Can't open template '$filename': $!"; + + $template->param( + is_remote_sup => LJ::SUP->is_remote_sup ? 1 : 0, + errors => $class->error_list, + form_auth => LJ::form_auth(), + siteroot => $LJ::SITEROOT, + ); + + $class->prepare_template_params($template, \%opts); + + return $template->output; +} + +1;