模板工具包字符编码

发布于 2024-10-08 23:56:48 字数 1293 浏览 8 评论 0原文

模板工具包似乎无法正确处理编码。

我传递 template->process 一个文件名(在哪里获取模板)、一个哈希引用(包含所有参数)和一个标量引用(在哪里放置输出),然后我返回然后将其显示给用户。

当我给它一个带有变音符号的字符串时,html 输出包括一个黑色菱形,每个字母都用白色问号代替(但字母数量正确)。任何其他角色都很好。

在调用 template->process 之前,我使用警告打印出字符串,此时一切都很好,据我所知,这是在 template->process 期间称事物变成垃圾。

有什么想法吗? 我尝试过使用 ENCODING =>; “utf8” 以及 binmode => ":utf8" 但对输出没有任何影响。

这是我的代码,删除了一些内容只是为了显示我对 template->process 的调用,请注意,如果我省略了 {binmode =>; 'utf8'} 它没有效果。

<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;

已解决 嘿,非常感谢您的回答,问题是,模板进程完成其工作后,我们然后在输出之前将字符串写入临时文件,因此我们还需要为文件,代码现在看起来像:

<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
binmode( STDOUT, ":utf8" );
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;

谢谢大家的宝贵时间:)

it seems like template toolkit isn't handling encoding properly.

I am passing template->process a filename (where to get the template), a hash reference (containing all the parameters), and a scalar reference (where to put the output) then I am returning that and in turn displaying it to the user.

When I give it a string with umlauts in it, the html output includes a black diamond with a white question mark in place of every letter (but the correct number of letters). Any other character comes out fine.

I am using a warn to print out the string before I make the call to template->process and at this point it is fine, from what I can tell it is during the template->process call that things get turned into garbage.

Any ideas?
I have tried using ENCODING => "utf8" as well as binmode => ":utf8" but neither have any affect on the output.

Here is my code with some of the fat trimmed out just to show my call to template->process, note that if I leave out the {binmode => 'utf8'} it has no effect.

<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;

SOLVED
Hey all thanks for your answers, the problem turned out to be that after template process had done its thing, we then wrote the string to a temporary file before outputting it so we also needed to set binmode for the file, the code now looks like:

<put variables in hash referenced to by vars>
<print out variables in has referenced to by $var>
my $data;
binmode( STDOUT, ":utf8" );
$template->process( $self->filename, $vars, \$data, {binmode => ':utf8'}) || die "Template process failed: ", $template->error();
return $data;

I thank you all for your time :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-10-15 23:56:48

下面的代码有效。 $data,具体来说,包含的字符串必须是 Perl 字符串,即正确的 解码d。请参阅官方文档中的编码介绍

use Template '2.21_02';

my $tt = Template->new({
    ENCODING     => 'utf8',
    # other options …
});

$tt->process(
    $template, $data, $output, {binmode => ':utf8'}
) or die $tt->error . ' in ' . $template;

The code below works. $data, specifically the strings contained must be Perl strings, i.e. properly decoded. See the introduction to encoding in the official documentation.

use Template '2.21_02';

my $tt = Template->new({
    ENCODING     => 'utf8',
    # other options …
});

$tt->process(
    $template, $data, $output, {binmode => ':utf8'}
) or die $tt->error . ' in ' . $template;
忆沫 2024-10-15 23:56:48

我的解决方案是平底船,所有非拉丁字符都进入 en.po 并在运行时通过 [% loc('string') %] 包含在内,这对我来说效果很好,因为我的模板有无论如何都要本地化。

My solution was to punt, and all non-latin characters go in en.po to be included at runtime via [% loc('string') %] and that works fine for me since my templates had to be localized anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文