Re: [Perl-widget-developer] Teambuilding Exercise #2: Usage - Template Toolkit concept
Status: Alpha
Brought to you by:
spadkins
|
From: Gunther B. <gu...@ex...> - 2001-06-11 04:45:20
|
At 08:09 PM 6/7/2001 +1000, Cees Hek wrote:
>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...
This seems OK. I also think the getting the value() will not be oft used
unless you need to encode logic to display a widget only if another widget
has a particular value.
I do prefer a taglibs metaphor as I posted earlier. With taglib support, I
can write a widget iterator that know to populate the widget tag with the
appropriate widget.
eg from one of my JSPs
<view:widgetIterator>
<view:next>
<TD CLASS="FIRST"><widget/></TD>
</view:next>
<view:next>
<TD CLASS="ALTROW"><widget/></TD>
</view:next>
</view:widgetIterator>
Of course, the exact syntax of taglibs can be argued about, but from a
functional level, I find them quite useful encapsulations of code. It's
really wonderful when template languages support something like it and it
goes hand in hand with widgets.
|