Re: [htmltmpl] Sub-Classing HTML::Template v2.7
Brought to you by:
samtregar
From: Mathew R. <mat...@re...> - 2005-03-06 23:49:09
|
There are some places in H::T where this is done... and I agree with = you -> its wrong if you need to subclass H::T... However, you should be able to simply change it to: $self->_new_from_loop(...) Perl will automatically dereference $self so as to determine the correct = package name - in fact ref($self) may return a string which is not = appropriate, in certain scenarios (eg: it may return HTML::Template when = it should return, say, MyHtmlTemplate). Then _new_from_loop should = implement: sub _new_from_loop { my $proto =3D shift; my $class =3D ref($proto) || $proto; ... } at this point $class will contain the correct package name. The same point could be said about H::T::E::output(), since it does = this: sub output { .... HTML::Template::output(...) ... } which again is incorrect if H::T::E is to be subclass'able. FWIW, the version of H::T that I have modified (available at: = http://members.optusnet.com.au/~mathew ) also makes this mistake in some = of the code that I have extended... ie we are all fallable... regards, Mathew ----- Original Message -----=20 From: "Cory Trese" <Ct...@on...> To: "Cory Trese" <Ct...@on...>; = <htm...@li...> Sent: Saturday, March 05, 2005 4:59 AM Subject: RE: [htmltmpl] Sub-Classing HTML::Template v2.7 > Hello, >=20 > I have found the root of my problem! All the code below was centered > around trying, and failing, to fully replace all instances of the > HTML::Template 'param( )' method within our code-base. I thought that > by building a sub-class and overriding param, I would be able to > accomplish this. However, I was unable to override all calls to = 'param( > )' -- some within HTML::Template remained using the SUPER::param( ). >=20 > I attempted to overwrite the package method, as seen below. This, > however, doesn't actually work. So again, no dice. Until I found the > following code in HTML::Template around Line #2112: >=20 > $loop->[HTML::Template::LOOP::TEMPLATE_HASH]{$starts_at} >=20 > =3D HTML::Template->_new_from_loop( >=20 > This does not seem friendly to sub-classes ... I didn't see any > references to this type of issue in Damian Conway's Object Oriented = Perl > book, but I found a solution that works great for my sub-classes. >=20 > $loop->[HTML::Template::LOOP::TEMPLATE_HASH]{$starts_at} >=20 > =3D ref( $self )->_new_from_loop( >=20 > This way, the "_new_from_loop" call always occurs against the correct > class -- HTML::Template or otherwise. I have no production code to = test > compatibility with HTML::Template::Expr so I cannot state if this type > of change will work for everyone trying to sub-class HTML::Template. >=20 > However, I do know that making this change caused my sub-classes to > completely work and I am very happy about that. >=20 > Sincerely, > Cory Trese > Lead Web Application Developer > =20 > O'NEIL & ASSOCIATES, INC. > 495 Byers Rd. > Miamisburg, Ohio 45342-3662 > Phone: (937) 865-0846 ext. 3038 > Fax: (937) 865-5858 > E-mail: [ct...@on...] >=20 >=20 > |> -----Original Message----- > |> From: htm...@li...=20 > |> [mailto:htm...@li...] On=20 > |> Behalf Of Cory Trese > |> Sent: Thursday, March 03, 2005 11:32 AM > |> To: htm...@li... > |> Subject: [htmltmpl] Sub-Classing HTML::Template v2.7 > |>=20 > |> Hello, > |>=20 > |> From some previous traffic on this list, Sam Tregar said: > |>=20 > |> |> Ultimately HTML::Template v2 doesn't offer much in terms=20 > |> of official=20 > |> |> support for sub-classes and extensions. That's a=20 > |> deficiency I intend=20 > |> |> to address in the perpetually-delayed v3. > |> =20 > |> I am trying to create a sub-class of HTML::Template that=20 > |> over-rides the 'param( )' method. The code below "works" --=20 > |> mostly -- but I have a feeling that I'm doing this all=20 > |> wrong. Does anyone have some advice or a link on=20 > |> HTML::Template v2 sub-classing? I looked at=20 > |> "HTML::Template::Expr" -- my code below is based, roughly,=20 > |> on this module. > |>=20 > |> My major questions are: > |>=20 > |> 1. How does a sub-class of HTML::Template construct itself? =20 > |> Should I over-ride 'new( )' or use something like 'make_new(=20 > |> )' to wrap 'new( )' > |> or 'SUPER::new( )'. > |>=20 > |> 2. What packages should my 'param( )' method be overridden=20 > |> in? More than HTML::Template? > |>=20 > |> The thing that doesn't work right: > |>=20 > |> 1. calls to 'param( )' seem to loose my 'i18n_strings'=20 > |> object -- it isn't ALWAYS present in 'options' when I call=20 > |> 'param( )', just most of the time. =20 > |>=20 > |> I _think_ that this is somehow related to some of the=20 > |> 'param( )' methods I'm using being in the class 'make_new(=20 > |> )' returns, and others, constructed somewhere else, but=20 > |> still using my 'param( )' method. I know that's confusing I=20 > |> haven't slept much in the past few days, sorry. > |>=20 > |> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > |> package Service::Template; > |>=20 > |> use strict; > |> use vars qw($VERSION); > |>=20 > |> $VERSION =3D '0.01'; > |>=20 > |> use HTML::Template; > |>=20 > |> use base 'HTML::Template'; > |>=20 > |> sub make_new { > |> my $pkg =3D shift; > |> my $self; > |>=20 > |> # check hashworthyness > |> croak("Service::Template->new() called with odd number of=20 > |> option parameters - should be of the form option =3D> value") > |> if (@_ % 2); > |> my %options =3D @_; > |>=20 > |>=20 > |> # create an HTML::Template object, catch the results to keep = error > |> # message line-numbers helpful. > |> eval { > |> $self =3D $pkg->new(%options ); > |> }; > |> croak("Service::Template->new() : Error creating=20 > |> HTML::Template object > |> : $@") if $@; > |>=20 > |> $self->{options}->{i18n_strings} =3D $options{'i18n_strings'}; > |>=20 > |>=20 > |> return $self; > |> } > |>=20 > |> 1; > |>=20 > |> package HTML::Template; > |>=20 > |> sub param { > |> ... > |> } > |>=20 > |> 1; > |>=20 > |> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > |>=20 > |> ************************************************************* > |> ********* > |> Confidentiality Notice > |> The information contained in this e-mail is confidential and=20 > |> intended for use only by the person(s) or organization=20 > |> listed in the address. If you have received this=20 > |> communication in error, please contact the sender at O'Neil=20 > |> & Associates, Inc., immediately. Any copying, dissemination,=20 > |> or distribution of this communication, other than by the=20 > |> intended recipient, is strictly prohibited. > |> ************************************************************* > |> ********* > |>=20 > |>=20 > |>=20 > |> ------------------------------------------------------- > |> SF email is sponsored by - The IT Product Guide Read honest=20 > |> & candid reviews on hundreds of IT Products from real users. > |> Discover which products truly live up to the hype. Start reading = now. > |> http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > |> _______________________________________________ > |> Html-template-users mailing list > |> Htm...@li... > |> https://lists.sourceforge.net/lists/listinfo/html-template-users > |>=20 >=20 > ********************************************************************** > Confidentiality Notice > The information contained in this e-mail is confidential and intended = for > use only by the person(s) or organization listed in the address. If = you have > received this communication in error, please contact the sender at = O'Neil & > Associates, Inc., immediately. Any copying, dissemination, or = distribution > of this communication, other than by the intended recipient, is = strictly > prohibited. > ********************************************************************** >=20 >=20 >=20 > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real = users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=3D6595&alloc_id=3D14396&op=3Dclick > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |