sub render {my $self = shift;my $body = shift // '';# status code and content type logic taken from parent class# See Kelp::Response::render()# Set code 200 if the code has not been set$self->set_code(200) unless $self->code;# If no content_type is set, then set it based on# the type of $body - JSON or HTML.unless ($self->content_type) {ref($body) ? $self->json : $self->html;}# sticky formsif ($self->code == 200 && $self->content_type =~ m{text/html}i) {if ($body =~ /<\s*form/i) {$body = HTML::FillInForm->fill(\$body, $self->app->req);}}return $self->SUPER::render($body, @_);}
I haven't used HTML::FillInForm in years (probably since the days of CGI). I would probably have my template engine handle the population and default values of the form. If you have set your mind on using HTML::FillInForm, perhaps a better way would be to create a render_form method and use it for those routes that contain forms. If you want a catch-all render that always handles forms, then your approach might be the right one.At the end, of course, it's whatever works for you best.As for your sub-classed render, it seems to me that it is repeating some of the code that it already inherits. Perhaps a more compact version, which uses HTML::FillInForm would be:sub render {my $self = shift;my $body = shift // '';# sticky formsif ($self->code == 200 && $self->content_type =~ m{text/html}i) {if ($body =~ /<\s*form/i) {$body = HTML::FillInForm->fill(\$body, $self->app->req);}}return $self->SUPER::render( $body, @_ );}
sub home { my $self = shift; $self->template( 'home', $self->req->parameters->as_hashref ); }
<form> <input name="email" value="[% email %]"> <input name="first_name" value="[% first_name %]"> <input name="last_name" value="[% last_name %]"> <input name="phone" value="[% phone %]"> </form>
Yes, your pseudo code is correct. If you used Template::Toolkit, I believe it does the encoding for you. There is a ready Kelp module for it. Look up Kelp::Module::Template::Toolkit.
Stefan