Domanda

Puoi aiutarmi per favore? Alla pagina Web, ho bisogno di stampare questo:

Team 1 vs Team 2

Team 3 vs team 4

Team 5 vs team 6

Team 7 vs Team 8

Il mio codice:

<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>
.

Risultato: la mia pagina Web è vuota. Che cosa sto facendo di sbagliato? Vedo nella console di debug:

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]}
.

È stato utile?

Soluzione

Credo che tu stia specificando correttamente il charset nella tua risposta HTTP. Innanzitutto, dal momento che hai un testo cirillico incorporato nella tua pagina .yaws, assicurati che il tuo editor lo stia salvando come UTF-8. Come realizza questo dipende dal tuo ambiente di sviluppo e dall'editor, ma io uso Emacs, quindi ho appena aggiunto il seguente commento html alla parte superiore del file .yaws:

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

Il problema successivo è ottenere correttamente il set di caratteri negli intestazioni HTTP della risposta. Per farlo, impostare l'intestazione Content-Type in modo appropriato nella tua risposta come questa:

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

Nota che l'intero tipo di ritorno è un'elenco Erlang. La prima tupla nell'elenco cancella l'intestazione di Content-Type predefinita che Yaws si applica per impostazione predefinita alle pagine .yaws. La seconda tupla nell'elenco specifica il valore corretto per Content-Type incluso il charset UTF-8. Il terzo elemento dell'elenco è la stessa tupla ehtml che hai specificato nella dichiarazione del tuo problema sopra.

BTW È possibile creare Yaws con UTF-8 come il tappetino predefinito eseguendo lo script configure Yaws come mostrato di seguito:

configure --with-defaultcharset=UTF-8
.

Dopo di ciò, ricostruire gli yaws con make come al solito. Se lo fai, le due tuple per impostare il Content-Type della tua risposta non sono più necessarie, poiché UTF-8 sarebbe il charset predefinito per tutte le risposte.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top