雅司西里尔输出

时间:2014-02-26 02:10:29

标签: yaws erlangweb

你能帮帮我吗? 在网页上,我需要打印一下:

第1队与第2队

第3队与第4队

第5队与第6队

第7队与第8队

我的代码:

<erl>
first_team_write(Head) ->
  {ehtml,
    %[{text,[], f("~p vs ", [Head])}]}.        % try
    [{p,[], [Val, " vs "]} || Val <- [Head]]}. % different
second_team_write(Head) ->
  {ehtml,
    %[{text,[], f("~p", Head)},                % ways
    [{p,[], [Head]},                           %
    {p,[], []}]}.

write_teams([], Num) ->
  Num;

write_teams([Head|Tail], Num) ->
if
  (Num rem 2) /= 0 ->
    first_team_write([Head]),
    io:format("~p vs ", [Head]),  %% debug
    write_teams(Tail, Num+1);
  (Num rem 2) == 0 ->
    second_team_write(Head),
    io:format("~p~n", [Head]),  %% debug
    write_teams(Tail, Num+1)
end.


out(A) ->
  application:start(odbc), 
  ConnString = 
  %"Driver={MySQL ODBC 5.2 ANSI Driver};" ++
  "Driver={MySQL ODBC 5.2 Unicode Driver};" ++
  "Server=127.0.0.1;Database=erandom;" ++ 
      "User=root;Password=1q2w3e;" ++ 
      "Option=3;" ++
      "CharSet=utf8;",
      {ok, Conn} = odbc:connect(ConnString, []), 
      % Cyrillic results
      Results = odbc:sql_query(Conn, "select team_name from teams where id in (select team_id from leagues where league_name = 'First League')"), 
      %{selected, [Selector], Results} = odbc:sql_query(Conn, "select team_name from teams where team_name in ('Sporting', 'Old School')"),
      %{selected, [Selector], Results} = odbc:sql_query(Conn, "select team_name from teams where id in (select team_id from leagues where league_name = 'First League')"),
      odbc:disconnect(Conn),
      application:stop(odbc),
      TeamList = element(3, Results),
      {ehtml,
        [{h4,[], "Пары этого тура:"},
        {hr},
          write_teams(TeamList,1)
      ]}.

</erl>

结果:我的网页为空白。 我究竟做错了什么? 我在调试控制台中看到:

1>
=INFO REPORT==== 25-Feb-2014::23:08:27 ===
    application: odbc
    exited: stopped
type: temporary
1> {[208,159,209,128,208,190,208,188,208,181,209,130,208,181,208,185]} vs 1> {[208,161,208,145,208,162]}
1> {[208,158,208,187,208,184,208,188,208,191]} vs 1> {[208,159,209,128,208,190,208,179,209,128,208,181,209,129,209,129]}
1> {[208,164,208,154,32,208,148,209,142,208,182,208,184,208,189,208,176]} vs 1> {"Old school"}
1> {[208,161,208,191,208,176,209,128,209,130,208,176,208,186]} vs 1> {[208,160,208,190,209,130,208,190,209,128]}
1> {"Sporting"} vs 1> {[208,161,208,190,208,191,209,128,208,190,209,130,208,184,208,178,208,187,208,  181,208,189,208,184,208,181]}

1 个答案:

答案 0 :(得分:1)

我相信您没有在HTTP响应中正确指定字符集。首先,由于您的.yaws页面中嵌入了西里尔文本,因此请确保您的编辑器将其保存为UTF-8。如何实现这一点取决于您的开发环境和编辑器,但我使用emacs,所以我只是将以下HTML注释添加到.yaws文件的顶部:

<!-- -*- coding: utf-8 -*- -->

下一个问题是在响应的HTTP头中正确设置字符集。为此,请在回复中正确设置Content-Type标题,如下所示:

[{header, {content_type, erase}},
 {header, {content_type, "text/html; charset=UTF-8"}},
 {ehtml,
  [{h4,[], "Пары этого тура:"},
   {hr},
      write_teams(TeamList,1)
  ]}].

请注意,整个返回类型是Erlang列表。列表中的第一个元组将删除Yaws默认应用于.yaws页面的默认Content-Type标题。列表中的第二个元组指定Content-Type的正确值,包括UTF-8字符集。列表的第三个元素是您在上面的问题陈述中指定的ehtml元组。

顺便说一句,您可以通过运行Yaws configure脚本将UTF-8作为默认字符集来构建Yaws,如下所示:

configure --with-defaultcharset=UTF-8

之后,像往常一样用make重建Yaws。如果这样做,则不再需要用于设置回复的Content-Type的两个元组,因为UTF-8将是所有回复的默认字符集。