Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Re: a survey of templates (http://www.velocityreviews.com/forums/t955060-re-a-survey-of-templates.html)

J. Gleixner 12-03-2012 04:07 PM

Re: a survey of templates
 
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.


Hans Mulder 12-04-2012 11:27 AM

Re: a survey of templates
 
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



Peter J. Holzer 12-05-2012 12:21 AM

Re: a survey of templates
 
On 2012-12-04 11:27, Hans Mulder <hansmu@xs4all.nl> 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
| | | hjp@hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel

Uri Guttman 12-05-2012 07:25 AM

Re: a survey of templates
 
>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:

PJH> On 2012-12-04 11:27, Hans Mulder <hansmu@xs4all.nl> 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

Uri Guttman 12-06-2012 02:30 AM

Re: a survey of templates
 
>>>>> "CD" == Cal Dershowitz <cal@example.invalid> 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

Charlton Wilbur 12-10-2012 05:27 PM

Re: a survey of templates
 
>>>>> "CD" == Cal Dershowitz <cal@example.invalid> 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
cwilbur@chromatico.net


All times are GMT. The time now is 01:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.