我想创建一个根据给定参数和 map 生成 HTML 的服务。给定参数,服务在 map 中搜索 html,以及在客户端启动的函数。
type sample =
(string (* little text *)*
Html5_types.html Eliom_content.Html5.elt (* html page *) *
(unit -> unit)(* Demonstration function *))
鉴于该功能要在客户端启动,我将其作为客户端值插入到 map 中:
{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:主要:致命 - 配置文件中的错误:解析配置文件时出错:Eliom:加载 local/lib/examples/examples.cma 时:失败(“无法在此处调用该函数,因为它需要信息”关于请求或网站。")
我的第一个猜测是,这是因为我将客户端值存储在服务之外,但是有什么方法可以将这种值存储在服务器上吗?
我尝试将它们包装在常规函数中:
让 demo_serv_func () = {unit{demo_client_func ()}}
但问题仍然存在......
最佳答案
我发现了这个问题。问题不是因为我存储了客户端函数,而是因为我在服务之外使用了 Eliom_tools.F.html
。
Eliom_tools 恰好需要服务的上下文才能运行,并且由于我将其存储在服务之外,因此它无法工作。
我通过在服务内使用 Eliom_tools 并将 HTML 页面的正文存储在 map 中解决了该问题。
关于ocaml - 由于故障 : ("That function cannot be called here because it needs information about the request or the site."),无法启动 ocsigen 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18023145/