我想根据给定的参数和地图创建一个生成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 ()}}
但问题仍然存在......
答案 0 :(得分:2)
我发现了这个问题。问题不是因为我存储了客户端功能,而是因为我在服务之外使用了Eliom_tools.F.html
。
Eliom_tools需要服务的上下文才能运行,因为我将它存储在服务之外,所以它无法工作。
我通过在服务中使用Eliom_tools并在地图中存储HTML页面的主体来解决了这个问题。