Re: [Perl-widget-developer] Teambuilding Exercise #2: Usage - Template Toolkit concept
Status: Alpha
Brought to you by:
spadkins
From: Cees H. <ce...@si...> - 2001-06-06 23:22:32
|
On Wed, 6 Jun 2001, Stephen Adkins wrote: > At 09:53 AM 6/6/2001 -0400, Jay Lawrence wrote: > > > >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"; > > Hmmm. > So the "widgets" entry in the stash becomes magic. > Seems unattractive because you need to modify the Template Toolkit itself. > But I every solution I can think of requires more steps in the Templates > page than > I'd like to see as well. Also, you want to be able to access a widget's value, not just render it... I did a quick TT2 test last week with the first code base, and I thought an easy and very flexable way to use it was as follows: use Template; use Widget::Test; my $tt=Template->new( { INCLUDE_PATH=>"." } ); $vars= { 'test1' => Widget::Test->new( maximum=>100, current=>10 ) 'lots' => 'o', 'more' => 'variables', }; $tt->process("test.html", $vars) || die "Failed: ".$tt->error()."\n"; then in you template you could just do: This is a test widget: [% test1.render %]<br> And here is it's value [% test1.value %]<br> This doesn't require any special code in the Widget library or the Template Toolkit. However, it does require a template designer to know a little bit about widgets (ie how to render them and get their values) I had a working example of this, but it is at home (and I am at work). I can fix it up an post it for comments if everyone wants... Cees Hek > > >-------------- > >test.html > >-------------- > >This is a test. <p> > >[% test1 %] > > What do you see as the desirability of making the call program know about the > widgets vs. making the template know about the widgets? > > I had envisioned some usage like... > > [% USE wc = Widget %] > This is a test. <p> > [% wc.test1 %] > > This would need some sort of Template Toolkit driver or a special controller > for Template Toolkit to make the syntax work out this way. > > >-------------- > >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 > > } > > > > This is an interesting option. > Please see if you can come up with a way which does not require us to > modify the Template Toolkit to specifically recognize a "magic" variable > in the stash. > > Stephen > > > > _______________________________________________ > Perl-widget-developer mailing list > Per...@li... > http://lists.sourceforge.net/lists/listinfo/perl-widget-developer > > -- Cees Hek SiteSuite Corporation ce...@si... |