Gossamer Forum
Home : General : Perl Programming :

HTML::Template to GT::Template

Quote Reply
HTML::Template to GT::Template
I wonder if anyone here could help me to covert parser from HTML::Template to GT::Template
i now only able to do a single string replacement. while has difficulty in loop.

the orginal code is
sub display_cart {
my ($cgi, $session) = @_;
# getting the cart's contents
my $cart = $session->param("CART") || [];
my $total_price = 0;
my $RV = qq~<table><tr><th>Title</th><th>Price</th><th>Quantity</th></tr>~;
if ( $cart ) {
my $idx = 0;
for my $product ( @{$cart} ) { $idx++;
#$total_price += $product->{price};
$subtotal = ($product->{price})*($product->{quantity});
$total_price += $subtotal;
# my $idx = 0;
$RV .= qq~<tr><td>$product->{name}</td><td>$product->{price} ($idx++ $idx )</td><td>$subtotal----$product->{quantity}</td></tr>~;
}
} else {
$RV .= qq~
<tr>
<td colspan="2">There are no items in your cart
yet</td>
</tr>~;
}
$RV .= qq~
<tr>
<td><b>Total Price:::</b></td>
<td><b>$total_price></b></td>
</tr></table>~;
return $RV;

}
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
when using HTML::Template, i got
sub display_cart{
my ($cgi, $session) = @_;
my $template = new HTML::Template(filename=>"F:/webtest/test/cgi-bin/help3/test/some1.tmpl",
associate=>$session,
die_on_bad_params=>0);

return $template->output();
}




So how is the script for the GT::Template ?

<!-- shopping cart starts-->
<p>Here are your cart:<br><br>
<%loop cart_loop%>
<a href="<%name%>"><%price%></a>
<%endloop%>
<!-- shopping cart ends -->
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
Quote:
So how is the script for the GT::Template ?

<!-- shopping cart starts-->
<p>Here are your cart:<br><br>
<%loop cart_loop%>
<a href="<%name%>"><%price%></a>
<%endloop%>
<!-- shopping cart ends -->

I dunno. Your code is for H::T yet your template is for GT::Template. What happens when you try to run it?

On a side note; [nudge]I sure would be nice to see GT::Template on CPAN...[/nudge] :)

~Charlie

Last edited by:

Chaz: Oct 8, 2004, 10:53 AM
Quote Reply
Re: [Chaz] HTML::Template to GT::Template In reply to
Dear Charlie
Yes, the code is for HTML::Template , while i am unable to write
the script for GT::Template. so i ask for help here.

HTML::Template works well, while i am interesting in know how to write the script code
for GT::Template.
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
This is untested but might work:

Code:
sub display_cart {
my ($cgi, $session) = @_;

# getting the cart's contents
my $vars;
$vars->{total_price} = 0;

foreach my $product ( @{$session->param('CART')} ) {
$product->{prod_subtotal} = $product->{price} * $product->{quantity};
$vars->{total_price} += $product->{prod_subtotal};
push @($vars->{cart_loop}), $product;
}

require GT::Template;
return GT::Template->parse('html.tmpl', $vars);
}

and your template:

Code:
<%if cart_loop%>
<p>Here are your cart:</p>
<br><br>
<table>
<tr>
<td>Qty</td>
<td>Product</td>
<td>Unit Price</td>
<td>Product Subtotal</td>
</tr>
<%loop cart_loop%>
<tr>
<td><%quantity%></td>
<td><%name%></td>
<td><%price%></td>
<td><%prod_subtotal%></td>
</tr>
<%endloop%>
<tr>
<td colspan="3">Sub Total</td><td><%total_price%></td>
</tr>
</table>
<%else%>
<p>There are no items in your cart yet</p>
<%endif%>

Make sure you check out the pod for GT::Template. There's a *lot* you can do with it.

~Charlie

Last edited by:

Chaz: Oct 8, 2004, 2:53 PM
Quote Reply
Re: [Chaz] HTML::Template to GT::Template In reply to
Thanks Chaz,

i found the error when testing
syntax error at F:\webtest\test\cgi-bin\help3\test\session555.pl line 189, near "@($vars"
syntax error at F:\webtest\test\cgi-bin\help3\test\session555.pl line 194, near "}"
Execution of F:\webtest\test\cgi-bin\help3\test\session555.pl aborted due to compilation errors.
line 189 reads like below
push @($vars->{cart_loop}), $product;




anyway i now get a simaple to know where to start. thank.
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
Code:
@($vars->{cart_loop})

should be

Code:
@{$vars->{cart_loop}}
Quote Reply
Re: [Chaz] HTML::Template to GT::Template In reply to
Thanks Charlie , it now works great.
I added prod_listid tag to make it perfect when
there is a need to remove a cart item.

best of all . i have learn the way of adding new tag when
using GT-T from the sample code.


for my $product ( @{$session->param('CART')} ) {
$idx++;
$product->{prod_subtotal} = $product->{price} * $product->{quantity};
$product->{prod_listid} = $idx;
$vars->{total_price} += $product->{prod_subtotal};
push @{$vars->{cart_loop}}, $product;
}

so now it seems that i have a basic funcational script for cart function.

Thanks again Charlie for all the helps.

Last edited by:

courierb: Oct 10, 2004, 4:11 AM
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
In Reply To:
Thanks again Charlie for all the helps.

No worries :)

~Charlie
Quote Reply
Re: [courierb] HTML::Template to GT::Template In reply to
Congratulation for the successful upgrade from HTML::Template to GT::Template!
A few years ago probably I was the first, who did full GT::Template upgrade in Links 2.0.

GT::Template has several advantages, including the serious speed improvement, what GT::Template compiling gives...

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] HTML::Template to GT::Template In reply to
Quote:
Congratulation for the successful upgrade from HTML::Template to GT::Template! ... GT::Template has several advantages, including the serious speed improvement, what GT::Template compiling gives...

But one *huge* drawback is GT::Template is not GPL or "Artistic License" :/ I would love to be able to use it (and GT::SQL) in my other projects but unless GT releases it as GPL and/or "Artistic License" that's not an option.

~Charlie

Last edited by:

Chaz: Oct 21, 2004, 7:31 AM
Quote Reply
Re: [Chaz] HTML::Template to GT::Template In reply to
Well, I bought Links SQL, so got the right to use it :)
Altough it would be a good idea to have the GT::Template released to public as GPL.

I would like to see benchmark of GT::Template in this result list:
http://www.chamas.com/bench/
I would be curious how GT::Template speed compares to other template systems...

Maybe I will ask Alex to send a copy of GT::Template to have its benchmark included there.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] HTML::Template to GT::Template In reply to
Quote:
Well, I bought Links SQL, so got the right to use it :)

I have GT products too so I can use it for personal projects but I want to use it on paid jobs :) It's a shame they don't have it on CPAN yet. I find that GT::T has the gadgets that I personally find lacking in HTML::Template and it's not as huge and involved as Template::ToolKit. With H::T, I can just drop it in to a private lib for clients who can't get modules installed for one reason or another. You can't really do that with TT.

Quote:
Altough it would be a good idea to have the GT::Template released to public as GPL.

GT keeps mentioning that so mabye they'll get around to it one day.

Quote:
I would like to see benchmark of GT::Template in this result list:
http://www.chamas.com/bench/

Until they do make it GPL it's kind of pointless. It'll just make me drool more until they do release it. It's kind of like a carrot on a stick :)

~Charlie
Quote Reply
Re: [Chaz] HTML::Template to GT::Template In reply to
Quote:
GT keeps mentioning that so mabye they'll get around to it one day.
Did they?

If GT would make it GPL, I would put some efforts to add some features, but until they are keeping it private, I have no tto much chance to affect the development...


Quote:
Until they do make it GPL it's kind of pointless. It'll just make me drool more until they do release it. It's kind of like a carrot on a stick :)
I'm still curious what performance can I get with GT::Template.
I like its features, so I wouldn't change to other template system. However I want to know what is the maximum performance I can get out from it. It is needed for planning reasons. If you plan an application, it is necessary to know how scalable is. When you are scaling application it is not a good thing, if you need to change to another template system or another framework.
Therefore you have to know at the beginning of the development, how will you able to scale the app.

There is a good example, named Mason. It's not scalable enough. Their website says it's 2x or 3x slower than a mod_perl based script. Anyway, these results show worser proportion.
HTML::Mason simply can't compete against HTML::Template:
Code:
mod_perl 2000 h2000.pm 951.6 (it isn't a template system, just showed as reference)
Embperl v2.0b8 mod_perl 2000 h2000.epl 483.7 req/sec
Apache::ASP v2.53 mod_perl 2000 h2000.asp 438.3 req/sec
HTML::Template v2.6 mod_perl 2000 h2000.htmp 234.1 req/sec
HTML::Mason v1.19 mod_perl 2000 h2000.mas 51.9 req/sec
I believe that GT::Template can achieve more performance than Mason. If it would be faster than HTML::Template or even Apache::ASP, then it would really rocks. And since the templates are not just cached, but are compiled into Perl code, I see a good chance to achieve good performance.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] HTML::Template to GT::Template In reply to
Quote:
In Reply To:
Quote:
GT keeps mentioning that so maybe they'll get around to it one day.
Did they?

Yeah, Alex mentioned it a few times now. I just keep dropping (no so) subtle hints like this one as a reminder :)

~Charlie

Last edited by:

Chaz: Oct 22, 2004, 12:27 PM
Quote Reply
Re: [webmaster33] HTML::Template to GT::Template In reply to
Quote:
--------------------------------------------------------------------------------
Quote
--------------------------------------------------------------------------------

GT keeps mentioning that so mabye they'll get around to it one day.

--------------------------------------------------------------------------------

Did they?

Yeah, Alex mentioned it a little while ago (maybe as recent as a month ago). Lets just hope the do go ahead with it :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!