[PW-dev] 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-11 14:24:20
|
See bottom of message:
> At 11:09 AM 6/6/2001 -0400, 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.
> >
> > >--------------
> > >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
>
> What it sounds to me is that rather than hacking TT in a specific way for
> widgets, TT would benefit a lot from a generic taglib hook.
>
> In Java I am used to JSPs and we code widgets using taglibs. So the
widgets
> are objects, but then there is a widget taglib that can display a widget.
>
> <widget id='fname'/>
>
> in JSP parlance for example.
So, for example, with Template Toolkit might we have something like:
[% WIDGET fname %]
Which would render the widget in place - and of course you could do things
like [% WIDGET fname.value('Jay') %] if you wanted to set it in your
template code.
I don't know if it is being discussed but templates don't have a super
friendly way of expressing named parameters. You can do things like [%
WIDGET fname('value','Jay','colour','green','width','20') %] but it might be
more attractive to have a syntax this is clearly name=>"value" or the
like....
I guess the advantage here is that you can abstract how widgets are
instanciated and potentially create efficiency by allowing for widget
instanciation at the time the template is compiled...
Jay
|