[Templates] tpage and header line in a .txt file
Teun-Arno
A.Teeling3@chello.nl
Sat, 18 Nov 2006 15:42:20 +0100
This is a multi-part message in MIME format.
------=_NextPart_000_0002_01C70B28.22907B80
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit
Hello
Example_File : machines.txt
======================
name, os, ip, Connect_with, Customer
apollo, RedHat 7.3, 10.100.5.100, Telnet, Frieslandbank
hera, RedHat 7.2, 10.100.33.227, SSH, PON
juno, Solaris 8, 10.100.6.41 , SSH , SNV
artemis, RedHat 7.3, 10.100.6.42 , Telnet, BLA
And wanted from the "machines.txt" a .html file from it with headers in the
order that they are in the machines.txt file.
Wrote the next code to do so because I could not get at the headers ( name ,
os , ip ... ) in the order they are in the machines.txt file. (
machines.keys gave nothing back , machines.0.key gives them back in random
order , yes it's a hash. )
Template file : machines.tt
=====================
[% USE machines = datafile('machines.txt', delim = ',') %]
[% PERL %]
my @lables=();
my $headers;
open ( TXT , "< machines.txt " ) or die "no such file" ;
@lables=split(/\s*,\s*/,readline ( TXT )); # assume headers are on the
first line
close TXT;
chomp $lables[$#lables];
# export to the namespace of Template ?, it , seems to do so
$stash->set( headers => \@lables );
[% END %]
<HTML><BODY>
<Table border=1 cellpadding=0 cellspacing=0
STYLE="font-family:Arial;font-size:9px">
<TR>
[% FOREACH lable IN headers -%]
<TH bgcolor="yellow">[% lable %]</TH>
[% END -%]
</TR>
[% FOREACH machine IN machines -%]
<TR>
[%- FOREACH lable IN headers %]
[% IF machine.$lable == "" %]
<TD> </TD>
[%- ELSE -%]
<TD>[% machine.$lable %]</TD>
[%- END %]
[%- END %]
Now I must run it : tpage --eval_perl machines.tt
Is there a more elegant way to get at the headers in order they are in the
machines.txt.
Thanks in advance : Arno Teunisse
------=_NextPart_000_0002_01C70B28.22907B80
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dus-ascii">
<META content=3D"MSHTML 6.00.5700.6" name=3DGENERATOR></HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>Hello</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D203123114-18112006>Example_File :=20
machines.txt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006>name, =20
os, =20
ip, =20
Connect_with, Customer<BR>apollo, RedHat =
7.3, =20
10.100.5.100, =
Telnet, =20
Frieslandbank<BR>hera, RedHat 7.2, =20
10.100.33.227, =20
SSH, =20
PON<BR>juno, Solaris 8, =
10.100.6.41 =20
, SSH , =20
SNV<BR>artemis, RedHat 7.3, 10.100.6.42 , =20
Telnet, =
BLA</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>And =
wanted from=20
the "machines.txt" a .html file from it with headers in the =
order=20
that they are in the machines.txt file. </SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Wrote =
the next code=20
to do so because I could not get at the headers ( name , os , ip ... ) =
in the=20
order they are in the machines.txt file. ( machines.keys gave nothing =
back ,=20
machines.0.key gives them back in random order , yes it's a hash. )=20
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN =
class=3D203123114-18112006>Template file :=20
machines.tt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN class=3D203123114-18112006>[% =
USE machines =3D=20
datafile('machines.txt', delim =3D ',') %]<BR>[% PERL %]<BR>my=20
@lables=3D();<BR>my $headers;<BR> open ( TXT , "< =
machines.txt " )=20
or die "no such file" ;<BR> =
@lables=3Dsplit(/\s*,\s*/,readline (=20
TXT )); # assume headers are on the first line<BR> close=20
TXT; <BR> chomp $lables[$#lables];<BR> # export to the=20
namespace of Template ?, it , seems to do so<BR> $stash->set( =
headers=20
=3D> \@lables ); <BR>[% END =
%]<BR></SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006><HTML><BODY><BR><Table =
border=3D1=20
cellpadding=3D0 cellspacing=3D0=20
STYLE=3D"font-family:Arial;font-size:9px"><BR><TR><BR>[% =
FOREACH lable IN=20
headers -%]<BR><TH bgcolor=3D"yellow">[% lable %]</TH><BR>[% =
END=20
-%]<BR></TR><BR>[% FOREACH machine IN machines =
-%]<BR><TR><BR>[%-=20
FOREACH lable IN headers %]<BR>[% IF machine.$lable =3D=3D ""=20
%]<BR> <TD>&nbsp</TD><BR>[%- ELSE =
-%]<BR><TD>[%=20
machine.$lable %]</TD><BR>[%- END %]<BR>[%- END =
%]</SPAN></FONT></DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DCourier size=3D2><SPAN class=3D203123114-18112006>Now =
I must run it=20
: tpage --eval_perl machines.tt</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Is =
there a more=20
elegant way to get at the headers in order they are in the machines.txt. =
</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2><SPAN class=3D203123114-18112006>Thanks =
in advance :=20
Arno Teunisse</SPAN></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><SPAN=20
class=3D203123114-18112006></SPAN></FONT> </DIV></BODY></HTML>
------=_NextPart_000_0002_01C70B28.22907B80--