我想创建一个根据给定参数和地图生成其 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)
)
其余代码几乎只是经典 eliom-distillery 的结果,其中包含用于使用的客户端函数的 ojquery 包。编译阶段进展顺利,但是当我尝试启动服务器时,我收到以下错误消息:
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 ()}}
但问题依旧...