2

指定されたパラメーターとマップに従って HTML を生成するサービスを作成したいと考えています。パラメータを指定すると、サービスはマップ内で html を検索し、クライアント側で起動する関数を検索します。

type sample =
  (string (* little text *)*
   Html5_types.html Eliom_content.Html5.elt (* html page *) *
  (unit -> unit)(* Demonstration function *))

関数がクライアント側で起動されることを考えると、クライアント値としてマップに挿入します。

{client{
 let demo_function = ignore (Ojquery.add_html 
                             (Ojquery.jQ "li") "<p id='test1'>new paragraph</p>") }}

let get_samples () =
  let samples_map = Samples.empty in
  let samples_map = Samples.add "add_html"
    ("text",
     (Eliom_tools.F.html
       (** html stuff **)
      ),
   {unit->unit{demo_function}}) samples_map in
  samples_map

そして、次のようにサービスを登録します。

let sample_service =
  Eliom_service.service
    ~path:["examples"]
    ~get_params:Eliom_parameter.(string "entry")
  ()
let () =
  Examples_app.register
    ~service:sample_service
    (fun (entry) () ->
      try
        (let entry = Samples.find entry samples_map in
        let html = ((function (name, html, func) -> html) entry) in
        let func = ((function (name, html, func) -> func) entry) in
        ignore {unit{%func ()}};
        Lwt.return (html))
    with Not_found -> Lwt.return (not_found)
  )

コードの残りの部分は、使用されているクライアント関数の ojquery パッケージを含めた、古典的な eliom-distillery の結果にすぎません。コンパイル フェーズはスムーズに進みますが、サーバーを起動しようとすると、次のエラー メッセージが表示されます。

ocsigenserver: main: Fatal - Error in configuration file: Error while parsing configuration file: Eliom: while loading local/lib/examples/examples.cma: Failure("That function cannot be called here because it needs information about the request or the site.")

私の最初の推測では、サービスの外部にクライアントの値を保存しているという事実が原因であるということでしたが、この種の値をサーバーに保存する方法はありますか?

私は通常の関数でそれらをラップしようとしました: let demo_serv_func () = {unit{demo_client_func ()}}

しかし、問題は残りました...

4

1 に答える 1

2

問題が見つかりました。問題は、クライアント関数を保存したからではなくEliom_tools.F.html、サービスの外で使用したためです。

Eliom_tools が機能するにはサービスのコンテキストが必要であり、サービスの外部に保存していたため、機能しませんでした。

サービス内で Eliom_tools を使用し、HTML ページの本文をマップに格納することで問題を解決しました。

于 2013-08-05T15:20:20.173 に答える