Multiple lines of html store in variable non messy way

D

daveyand

Hey guys, cant find on the group what i am trying to do, i blam ethat
on the search terms.

Basically this is the issue:

$Report = "<html><head> <stlye> .greenbold{ font-weight:
bold;font-color: green;}.redbold{font-weight: bold;font-color: red;}";
$Report .= "</style></head>";

How can i change it so that i can do something like:

$Report <<EndOfHTML;
<html>
<head>
<stlye>
..greenbold{
font-weight: bold;
font-color: green;
}
..redbold{
font-weight: bold;
font-color: red;
}
</style>
</head>
EndOfHTML

you should see that it will be alot more readable.

Maybe i am doing something wrong, but i get errors when trying to do
the above.

Cheers Guys.
Also if this has been discussed can someone point me in the right
direction instead of people repeating thjeir answers to me
 
S

Sherm Pendley

daveyand said:
How can i change it so that i can do something like:

$Report <<EndOfHTML;

The only thing missing above is the '=' in your assignment:

my $Report = <<EndOfHTML;

More importantly though, it's not a very good idea to embed HTML within
your Perl code in the first place. Most of us who have been working with
these things for a while have gravitated towards a "template-based"
approach, where skeleton output is loaded from an external template,
which has placeholders for inserting dynamically-generated content.

My own preferred solution is the venerable Text::Template, although
there are dozens of modules on CPAN to choose from.

sherm--
 
A

A. Sinan Unur

Hey guys, cant find on the group what i am trying to do, i blam ethat
on the search terms.

I blame it on (bad) laziness.

....
Cheers Guys.
Also if this has been discussed can someone point me in the right
direction instead of people repeating thjeir answers to me

It is discussed in the FAQ:

perldoc -q "HERE documents"
 
T

Tad McClellan

daveyand said:
Maybe i am doing something wrong, but i get errors when trying to do
the above.


If you show us what you've tried, we could help you fix it.

If you don't, we can't.
 
T

Tad McClellan

daveyand said:
$Report = "<html><head> <stlye> .greenbold{ font-weight:
bold;font-color: green;}.redbold{font-weight: bold;font-color: red;}";
$Report .= "</style></head>";

How can i change it so that i can do something like:

$Report <<EndOfHTML;
<html>
<head>
<stlye>
.greenbold{
font-weight: bold;
font-color: green;
}
.redbold{
font-weight: bold;
font-color: red;
}
</style>
</head>
EndOfHTML


$report =~ tr/ \n/ /s; # squeeze whitespace
$report =~ s/(<[^>]*>)\s*/\n$1/g;
$report =~ s/(\.\w+{)\s*/\n$1\n/g;
$report =~ s/}/\n}/g;

Maybe i am doing something wrong,


Using regular expressions to "parse" HTML is wrong
in many cases.
 
D

daveyand

hey guys thanks for the help, what i posted was what i tried, didnt
think about the =.

The reason i am doing this is i am trying to send an html email to
myself. The idea above was to try and add css to the email but turns
out i cant do that, or at least i havent l;ooked into it properly.

As for the end result, i am using a function to create a template per
se when creating the main body of the email. This is what the main
body now looks like:

my $mainbody = <<EndOfHTML;
<table border="1" cellpadding="5" cellspacing="5" width="80%">
<tr>
<td>Ticker</td>
<td>Price</td>
<td>Change</td>
<td>Percentage</td>
<td>Start</td>
<td>Highest</td>
<td>Lowest</td>
<td>Time</td>
</tr>
<tr>
<td>$csv_stockinfo{"ticket"}</td>
<td>$csv_stockinfo{"price"}</td>
<td>$csv_stockinfo{"change"}</td>
<td>$csv_stockinfo{"percent"}</td>
<td>$csv_stockinfo{"start"}</td>
<td>$csv_stockinfo{"highest"}</td>
<td>$csv_stockinfo{"lowest"}</td>
<td>$csv_stockinfo{"time"}</td>
</tr>
<tr bgcolor="#ff2120">
<td colspan="2">&nbsp;</td>
<td>Quantity</td>
<td>Vesting Price</td>
<td>XCNG Rate</td>
<td>Sell All</td>
<td>Returns<td>Pounds</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td>$ShareAmount</td>
<td>\$$Initial_Price</td>
<td>$ExchangeRate</td>
<td>\$$Sell_Returns</td>
<td>\$$returns_minus_initial</td>
<td>&pound;$Final_Earnings</td>
</tr>
</table>
EndOfHTML

I get the error:

Can't find string terminator "EndOfHTML" anywhere before EOF at
../calstockreturns.pl line 183.

What am i doing wrong??
 
D

daveyand

fixed it, turns out and i didnt know this, but the EndOfHTML needs to
be not indented.

On a side not, how can i get the code to be indented properly when i
paste it up, as i think you guys would have seen this originally if it
had kept my indentation
 
S

Sherm Pendley

daveyand said:
On a side not, how can i get the code to be indented properly when i
paste it up

Use a real news reader (Thunderbird, Gnus, Agent, etc.) instead of
Google's web-based interface. Usenet is *far* older than the web, you
know...

sherm--
 
J

Juha Laiho

daveyand said:
fixed it, turns out and i didnt know this, but the EndOfHTML needs to
be not indented.

Please, read the perlfaq entry on here documents;
perldoc -q "here document"

The formal documentation is in perlop documentation page
perldoc perlop

.... and search for the second occurrence of <<EOF
(yes, clumsy).
 
I

ioneabu

Would this be approach be mostly instead of using CGI.pm functions? I
guess if you are not dealing with forms, using CGI.pm functions in a
Perl program is just like embedding HTML in Perl code.

So you could find a format you like at a site and copy the source,
modify it, and make it into your own template using Text;:Template?
That's pretty cool.

Thanks for the tip


wana
 
S

Sherm Pendley

Would this be approach be mostly instead of using CGI.pm functions? I
guess if you are not dealing with forms, using CGI.pm functions in a
Perl program is just like embedding HTML in Perl code.

Mostly, yes. There is one major exception though - HTML form elements.
CGI.pm has some great methods for creating new form elements that take
their default values from the incoming query. One of the most popular
uses for that is error-checking forms, where if there is a problem with
the input it one field, the same form comes back with that field
highlighted, and the others already filled out with the input you provided.

Even in that instance though, I'd most often use a template for the
surrounding page, with placeholders where the generated form elements
are to be placed.
So you could find a format you like at a site and copy the source,
modify it, and make it into your own template using Text;:Template?

Sure, you could do that. More to the point though, if you're working
with a designer, he can work on the HTML while you're working on the
logic. Or if you want to expand your team even further, you can have a
content developer working with strictly structured markup, a designer
styling it with CSS, and a programmer working on the logic.

Obviously there needs to be *some* coordination between the members of
the team above, but having everything in separate files gives a *lot*
more flexibility than you'd have with everything bundled up in your Perl
code. It's really a necessity, not a luxury once you start working on
bigger sites.

sherm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top