Thread Wunsch: HTML-Template-Compiled und Helferlein oder Makros
(10 answers)
Opened by GwenDragon at 2013-09-06 17:28
man kann das mit einem plugin machen.
HTCForm.pm:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 package HTCForm; use strict; use warnings; use Moose; has token => (is => 'rw', isa => 'Str'); has takeme => (is => 'rw', isa => 'Str'); HTML::Template::Compiled->register(__PACKAGE__); sub register { my ($class) = @_; my %plugs = ( tagnames => { HTML::Template::Compiled::Token::OPENING_TAG() => { FORM => [sub { exists $_[1]->{NAME} }, qw/ NAME METHOD URL TOKEN /], }, HTML::Template::Compiled::Token::CLOSING_TAG() => { FORM => [undef], }, }, compile => { FORM => { open => \&open_form, close => \&close_form, }, }, ); return \%plugs; } my @stack; sub close_form { my ($htc, $token, $args) = @_; my $OUT = $args->{out}; my $token_def = pop @stack; my @defs = split /,/, $token_def; my $expression = ""; for my $def (@defs) { if ($def eq 'TAKEME') { $expression .= <<"EOM"; $OUT '<input type="hidden" name="TAKEME" value="' . \$t->get_plugin('HTCForm')->takeme . '"> '; EOM } elsif ($def eq 'TOKEN') { $expression .= <<"EOM"; $OUT '<input type="hidden" name="TOKEN" value="' . \$t->get_plugin('HTCForm')->token . '"> '; EOM } } $expression .= qq{$OUT '</form>';\n}; return $expression; } sub open_form { my ($htc, $token, $args) = @_; my $OUT = $args->{out}; my $attr = $token->get_attributes; my $name = $attr->{NAME}; my $method = $attr->{METHOD}; my $url = $attr->{URL}; my $token = $attr->{TOKEN}; push @stack, $token; my $expression = <<"EOM"; $OUT '<form name="$name" method="$method" action="$url">'; EOM return $expression; } 1; skript: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 use HTML::Template::Compiled; use HTCForm; my $form = HTCForm->new; my $htc = HTML::Template::Compiled->new( filename => "form.html", plugin => [$form], debug => 1, ); $form->token('fd9d8aa21e71f7626d6cc35'); $form->takeme('1234567'); say $htc->output; $form->token('abc'); $form->takeme('123'); say $htc->output; template: Code: (dl
)
1 test ausgabe: Code: (dl
)
1 test in HTCForm::open_form wird token="TOKEN,TAKEME" auf den stack geschoben, damit vor dem ende des formulars die entsprechenden hidden fields (in close_form) eingefügt werden können. wenn die felder auch direkt nach dem form-open kommen können, brauchst du das mit dem stack nicht. Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wie frage ich & perlintro brian's Leitfaden für jedes Perl-Problem |