Hello,
I'm not sure whether this is the right place to post this, but as many other GT::Template-related questions show up here, I thought it just might be.
I'm using GT::Template in a private database manager with data in utf-8. One of the optional columns contains XML markup. The perl script I use to extract search results parses this markup with XML::LibXML and then adds the result value to a hash passed to a template.
The problem is: when I send parsed markup to GT::Template, it garbles all utf-8 characters in the *unparsed* column values. When I have a record without a value for the markup column, then things work fine. When I print directly, and not through columns, everything works fine.
This is with MySQL 4.1.1-alpha-standard, and perl 5.8.1.
This is the code of a test script:
use strict;
use DBI;
use XML::LibXML;
use GT::Template;
use vars qw ($dbh %rec);
&connect;
my $sql = qq|select * from pv_text where ID='5'|;
my $sth = $dbh->prepare($sql) || die ("can't execute: $DBI::errstr");
$sth->execute || die ("can't execute: $DBI::errstr");
while (my $hashref = $sth->fetchrow_hashref) {
%rec = %$hashref;
if ($rec{skt_notes}) {
$rec{skt_notes} = &construct_apparatus($rec{skt_notes});
}
}
print "Content-type: text/html\n\n";
print qq|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>UTF-8 oddities test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
<meta http-equiv="Expires" content="-1" />
</head>
<body><p>|;
GT::Template->parse_print("/srv/www/htdocs/istb/cgi-bin/pv3/template/test.tmpl", \%rec);
print qq|</p></body></html>|;
sub construct_apparatus {
my $app = shift;
my $string;
my $parser = XML::LibXML->new();
my $tree = $parser->parse_string($app);
my $root = $tree->getDocumentElement;
foreach my $app_el($root->findnodes('app')) {
my $lemma = $app_el->findvalue('lem');
my $wit = $app_el->findvalue('lem/@wit');
$string .= "Lemma: $lemma ($wit) <br />";
foreach my $rdg ($app_el->getChildrenByTagName('rdg')) {
my $reading = $rdg->getFirstChild->getData;
my $wit = $rdg->getAttribute('wit');
$string .= qq|Reading: $reading ($wit)<br />|;
}
}
return ($string);
}
&disconnect;
sub connect {
$dbh = DBI->connect("DBI:mysql:pv:localhost", "kellner", "somepassword", {RaiseError => 1, AutoCommit => 1});
}
sub disconnect {
$dbh->disconnect;
}
This is the template:
<table width="700" cellpadding="5" cellspacing="0" border="0"><tr><td valign="top" width="50%">
<p>
<%skt%>
</p>
<p class="small">
<%skt_notes%>
</p>
<p><%tib%></p>
</td><td valign="top">
<p>
<%trans%>
</p>
<p class="small">
<%trans_notes%>
</p>
</td></tr>
<tr><td colspan="2"><h3>Notes</h3><br /> <%notes%></td></tr>
</table>
I can provide some sql data if needed. But perhaps this is a simple and really stupid mistake (hopefully) ...
Thanks in advance,
kellner
I'm not sure whether this is the right place to post this, but as many other GT::Template-related questions show up here, I thought it just might be.
I'm using GT::Template in a private database manager with data in utf-8. One of the optional columns contains XML markup. The perl script I use to extract search results parses this markup with XML::LibXML and then adds the result value to a hash passed to a template.
The problem is: when I send parsed markup to GT::Template, it garbles all utf-8 characters in the *unparsed* column values. When I have a record without a value for the markup column, then things work fine. When I print directly, and not through columns, everything works fine.
This is with MySQL 4.1.1-alpha-standard, and perl 5.8.1.
This is the code of a test script:
Code:
#!/usr/bin/perl use strict;
use DBI;
use XML::LibXML;
use GT::Template;
use vars qw ($dbh %rec);
&connect;
my $sql = qq|select * from pv_text where ID='5'|;
my $sth = $dbh->prepare($sql) || die ("can't execute: $DBI::errstr");
$sth->execute || die ("can't execute: $DBI::errstr");
while (my $hashref = $sth->fetchrow_hashref) {
%rec = %$hashref;
if ($rec{skt_notes}) {
$rec{skt_notes} = &construct_apparatus($rec{skt_notes});
}
}
print "Content-type: text/html\n\n";
print qq|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>UTF-8 oddities test</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate" />
<meta http-equiv="Expires" content="-1" />
</head>
<body><p>|;
GT::Template->parse_print("/srv/www/htdocs/istb/cgi-bin/pv3/template/test.tmpl", \%rec);
print qq|</p></body></html>|;
sub construct_apparatus {
my $app = shift;
my $string;
my $parser = XML::LibXML->new();
my $tree = $parser->parse_string($app);
my $root = $tree->getDocumentElement;
foreach my $app_el($root->findnodes('app')) {
my $lemma = $app_el->findvalue('lem');
my $wit = $app_el->findvalue('lem/@wit');
$string .= "Lemma: $lemma ($wit) <br />";
foreach my $rdg ($app_el->getChildrenByTagName('rdg')) {
my $reading = $rdg->getFirstChild->getData;
my $wit = $rdg->getAttribute('wit');
$string .= qq|Reading: $reading ($wit)<br />|;
}
}
return ($string);
}
&disconnect;
sub connect {
$dbh = DBI->connect("DBI:mysql:pv:localhost", "kellner", "somepassword", {RaiseError => 1, AutoCommit => 1});
}
sub disconnect {
$dbh->disconnect;
}
This is the template:
Code:
<table width="700" cellpadding="5" cellspacing="0" border="0"><tr><td valign="top" width="50%">
<p>
<%skt%>
</p>
<p class="small">
<%skt_notes%>
</p>
<p><%tib%></p>
</td><td valign="top">
<p>
<%trans%>
</p>
<p class="small">
<%trans_notes%>
</p>
</td></tr>
<tr><td colspan="2"><h3>Notes</h3><br /> <%notes%></td></tr>
</table>
I can provide some sql data if needed. But perhaps this is a simple and really stupid mistake (hopefully) ...
Thanks in advance,
kellner