带Prolog的动态HTML
这是我的第一个问题,请原谅我的错误 我想从Prolog中的列表生成动态HTML。 对于每个问题,我想用这个问题生成一个p标签带Prolog的动态HTML,html,prolog,swi-prolog,Html,Prolog,Swi Prolog,这是我的第一个问题,请原谅我的错误 我想从Prolog中的列表生成动态HTML。 对于每个问题,我想用这个问题生成一个p标签 question(1, 'First Question'). question(2, 'Second Question'). get_quest( Q ) :- question( _, Q ). index(_Request) :- get_quest( Q ), reply_html_page( [ title('Dynamic HTM
question(1, 'First Question').
question(2, 'Second Question').
get_quest( Q ) :- question( _, Q ).
index(_Request) :-
get_quest( Q ),
reply_html_page(
[
title('Dynamic HTML')
],
[
h1('Questions'),
p( Q )
]
).
我知道这行不通,但我找不到正确的解决办法
更新
下面是我的代码实现。
谢谢大家的帮助
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/html_write)).
server(Port) :-
http_server( http_dispatch, [ port(Port) ]).
:- http_handler( root(.), index, [] ).
:- encoding( utf8 ).
get_quest( Q ) :- question( _, Q ).
index(_Request) :-
reply_html_page(
[
title('Questions')
],
[
h1('Dynamic HTML')
|\tables
]
).
tables -->
{ tables( Ls ) },
html( [ div( ul( Ls ) )]).
tables( Ls ) :-
findall( li( Q ), get_quest( Q ), Ls ).
我只是调试了一下这个。。。 我假设您已经设置了所需的基础结构
...
:- use_module(library(http/html_write)).
:- http_handler(/, hello_world, []).
...
hello_world(Request) :-
debug(wn_basic_gui, '~w', hello_world(Request)),
reply_html_page([\header, \jquery, \css_binding],
[\intro
,\tables
,\footer
,\folding_compound
]).
tables --> {tables(Ls)},
html([p(ul(Ls))]).
tables(Ls) :-
findall(li(\term(S,[])), schema_wn3_table(_,S), Ls).
tables/1创建嵌套的
元素
对于你的情况,应该很容易
index(_Request) :-
findall(p(Q), get_quest( Q ), Qs),
reply_html_page(
[
title('Dynamic HTML')
],
[
h1('Questions')|Qs
]
).
这是“已完成”的测试代码
:- module(so, [so/0]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- http_handler(/, index, []).
so :- http_server(http_dispatch, [port(1234)]).
question(1, 'First Question').
question(2, 'Second Question').
get_quest( Q ) :- question( _, Q ).
index(_Request) :-
findall(p(Q), get_quest( Q ), Qs),
reply_html_page(
[
title('Dynamic HTML')
],
[
h1('Questions')|Qs
]
).
你用的是哪一个序言?SWI Prolog拥有广泛的web开发库,包括html Generation.Hi Boris。是的,我正在使用SWI Prolog。是的,有用于生成html的库。但我不知道如何解决这个具体问题。这将与DCG有关@安迪,谢谢你把我的代码格式化。太好了!多谢各位。我们也在用findall进行实验,它看起来与您的解决方案非常相似,但没有想到将Q封装在p()中。再次感谢你!:)在学习Prolog时,我很好奇,当
findall
可以提取任何问题(Q)
时,为什么要使用单独的get\u quest
谓词。除了可读性,还有其他原因吗?