Re: [Perl-widget-developer] Teambuilding Exercise #2: Usage - Template Toolkit concept
Status: Alpha
Brought to you by:
spadkins
From: Jay L. <Ja...@La...> - 2001-06-06 13:52:19
|
Widgets in Template Toolkit - QED Here is something that I prototyped on my own system... - Just to show exactly how simple I'd like the widgets to appear to the template user ... General ideas: - Create hash of widgets: name => Widget::Object - Register this with Template::Toolkit somehow - two places come to mind - in the vars (as I have shown here) - At Template instantiation my $tt=Template->new ( { WIDGETS=>$widgetHashRef } ); - Reference widgets solely by name - I do find the "wc." - "wc.widget" - a bit redundant but understand its purpose - instead we can beef up the _dotop sub of Stash to give us what we want! - Trained Stash to look for widgets by name and if found call their "display" method with any parameters supplied in the template testprg.pl ------------- use Template; use Widget::Test; my $tt=Template->new( { INCLUDE_PATH=>"." } ); # Ya get yer vermin whereever u want - XML, SQL, FooBar $vars= { 'widgets' => { 'test1' => Widget::Test->new( maximum=>100, current=>10 ) }, 'lots' => 'o', 'more' => 'variables', }; $tt->process("test.html", $vars) || die "Failed: ".$tt->error()."\n"; -------------- test.html -------------- This is a test. <p> [% test1 %] -------------- Patch to Template Toolkit 2.02 - Template/Stash.pm -------------- if (defined($value = $root->{ $item })) { return $value unless ref $value eq 'CODE'; ## RETURN @result = &$value(@$args); ## @result } # Whacked in widget handler if (defined $root->{ 'widgets' } && defined $root->{'widgets'}{$item}) { return $root->{'widgets'}{$item}->display(@$args); } # /Whack elsif ($lvalue) { # we create an intermediate hash if this is an lvalue return $root->{ $item } = { }; ## RETURN } |