Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: a survey of templates

Reply
Thread Tools

Re: a survey of templates

 
 
J. Gleixner
Guest
Posts: n/a
 
      12-03-2012
On 12/03/12 02:53, Cal Dershowitz wrote:
> I've been reading up on template tutorials tonight. I'd like to discuss
> the tools that are available for these types of tasks in their
> diversity, as this is definitely one of those areas where there's more
> than one way to do it.


There are a lot of pages that offer examples/comparisons
between template engines.

>
> I'm very pleased with the result I got from the last thread:


Which has nothing to do with your question about templates, so
why bother posting it? .. complete waste of time...

....

> $ ./badger1.pl
> Template process failed: file error - src/greeting.html: not found
> $ cat badger1.pl
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Template;
>
> my $file = 'src/greeting.html';


You're specifying the file 'greeting.html' is going to be in the 'src'
directory of the directory this script is running from...


> my $vars = {
> message => "Hello World\n"
> };
>
> my $template = Template->new();
>
> $template->process($file, $vars)
> || die "Template process failed: ", $template->error(), "\n";
> $
>
> I look at this and say, doesn't html need to be the output rather than
> the input?


Huh? You're providing the 'process' method with the
template to use and the data. The 'process' method will print
the results to STDOUT.

>
> $ ls
> 01 ftp_bash1.pl~ secret2.tmpl secret.pl secret.tmpl~
> badger1.pl secret2.pl secret2.tmpl~ secret.pl~
> ftp_bash1.pl secret2.pl~ secret4.pl secret.tmpl


Your 'secret.tmpl' is in the same directory, it's not in
'src', as you specified. That's why there's a pretty
helpful error message being provided.

> $ ./secret.pl
> <!-- secret.tmpl -->
> <h1>Hello World</h1>
>
> $ cat secret.pl
> #!/usr/bin/perl -w
> use strict;
> use 5.010;
> # secret.pl
> use HTML::Template;
> my $bar = 'World';
> my $template = HTML::Template->new(filename => 'secret.tmpl');


'secret.tmpl' is in the current directory, so that's why this works.

> $template->param(SECRET_MESSAGE => $bar);
> print $template->output;
> $ cat secret.tmpl
> <!-- secret.tmpl -->
> <h1>Hello <TMPL_VAR NAME=SECRET_MESSAGE></h1>
>
> $
>
>
> As I look at this, I see things I would consider improvements. Number
> one, it's in perl not
>
> [ %
>
>
> % ]


Really.. '<h1>Hello <TMPL_VAR NAME=SECRET_MESSAGE></h1>' is Perl???


>
> Q1) Would I lose any of the functionality that I read about in the
> badger book if I used HTML::Template instead?
>
> Q2) What would I gain?


They're different ways of doing things. There are many others.

They all do similar things, some are better at certain things than
others. Pick the one you like/understand and try it.

 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      12-04-2012
On 4/12/12 07:59:28, Cal Dershowitz wrote:
> print $fh "<!DOCTYPE html>\n";
> print $fh '<html lang="en">' . "\n";
> print $fh "<head>\n";
> print $fh '<meta charset="utf-8">' . "\n";
> print $fh "<title>Operation Lasagna Chariot</title>\n";
> print $fh "</head>\n";
> print $fh "<body>\n";
> print $fh "<h1>one stays; one goes.</h1>\n";
> print $fh "<h2>take your pick!</h2>\n";


It would be more perlish to use a "here document":

print $fh <<'EOT';
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Operation Lasagna Chariot</title>
</head>
<body>
<h1>one stays; one goes.</h1>
<h2>take your pick!</h2>
EOT


Hope this helps,

-- HansM


 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-05-2012
On 2012-12-04 11:27, Hans Mulder <(E-Mail Removed)> wrote:
> On 4/12/12 07:59:28, Cal Dershowitz wrote:
>> print $fh "<!DOCTYPE html>\n";
>> print $fh '<html lang="en">' . "\n";
>> print $fh "<head>\n";
>> print $fh '<meta charset="utf-8">' . "\n";
>> print $fh "<title>Operation Lasagna Chariot</title>\n";
>> print $fh "</head>\n";
>> print $fh "<body>\n";
>> print $fh "<h1>one stays; one goes.</h1>\n";
>> print $fh "<h2>take your pick!</h2>\n";

>
> It would be more perlish to use a "here document":


Here documents cannot be indented and are therefore too ugly to use in
about 99% of all possible use cases.

hp


--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-05-2012
>>>>> "PJH" == Peter J Holzer <(E-Mail Removed)> writes:

PJH> On 2012-12-04 11:27, Hans Mulder <(E-Mail Removed)> wrote:
>> On 4/12/12 07:59:28, Cal Dershowitz wrote:
>>> print $fh "<!DOCTYPE html>\n";
>>> print $fh '<html lang="en">' . "\n";
>>> print $fh "<head>\n";
>>> print $fh '<meta charset="utf-8">' . "\n";
>>> print $fh "<title>Operation Lasagna Chariot</title>\n";
>>> print $fh "</head>\n";
>>> print $fh "<body>\n";
>>> print $fh "<h1>one stays; one goes.</h1>\n";
>>> print $fh "<h2>take your pick!</h2>\n";

>>
>> It would be more perlish to use a "here document":


PJH> Here documents cannot be indented and are therefore too ugly to use in
PJH> about 99% of all possible use cases.

not in my book. i like to see how the text will look on the left
margin. and in what way is that mess of prints, noisy quotes and such
cleaner than one here doc? and if you insist on indenting the text of
here docs there are several short tricks to removing the indent cleanly.

uri
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-06-2012
>>>>> "CD" == Cal Dershowitz <(E-Mail Removed)> writes:

CD> On 12/05/2012 12:25 AM, Uri Guttman wrote:

PJH> Here documents cannot be indented and are therefore too ugly to use in
PJH> about 99% of all possible use cases.
>>
>> not in my book. i like to see how the text will look on the left
>> margin. and in what way is that mess of prints, noisy quotes and such
>> cleaner than one here doc? and if you insist on indenting the text of
>> here docs there are several short tricks to removing the indent cleanly.


CD> Still fishing for what others have done with templating.

then checkout Template::Simple. fastest templater around and it is very
easy to use. only 4 markups and it can do most templating tasks.

uri
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      12-10-2012
>>>>> "CD" == Cal Dershowitz <(E-Mail Removed)> writes:

CD> When there's so many things going wrong in a script, I start to
CD> lose my moorings on file hierarchies and how to traverse and
CD> refer to them. I think it's tricky business, and I'm hoping to
CD> sort it out in a systematic way through the use of templating.

When there are so many things going wrong in a script, it's a sign that
you've bitten off more than you can chew. Remove everything in your
script that isn't working, and add *one* thing. Focus on it until it
works. Then add the second thing. Focus on it until it works, and
check to be sure you haven't broken the first thing.

CD> Ben, I sit there as I'm defining new things on the fly, and I
CD> say to myself "Ben is going to shake his head at that choice of
CD> this variable name," but that kind of indicates the problem in
CD> that I really don't know what I'm doing here and am making it up
CD> as I go.

You will confuse yourself less, and reveal problems sooner, if you
choose sensible variable names.

Charlton


--
Charlton Wilbur
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments