How to pass variables to html?

I’m using: SWI-Prolog version 8.2.4

I have an html page and want to change navbar depending on the user’s role. I use http_reply_file and need to pass the role to html. What is the best way to do that?

P.S. I wouldn’t want to rewrite the code for reply_html_page.

I think it depends on ow you integrate the navbar into the page and how you decide on the user role. For the first, do you use the reply_html_page/3 and the Style argument? There are so many ways to decide on the user and user role I hardly know what to ask …

If you can decide on the role from the HTTP request and/or authorization library, you can just write a predicate that does so and make the predicate that generates the part of the page that must be changed dependent on that.

I can get the role during the request, but my handler looks like:

editor(Request) :-
	http_reply_file('apps/src/editor.html', [], Request).

For example, I can do:

navbar(_Request) :-
      get_user_role(Role),
      reply_html_page([], [some code]).

Although I wonder how to integrate navbar into editor?

You can integrate the content of a file into an otherwise Prolog generated page using

include_html_file(File) -->
    load_html(File, DOM, []),
    html(DOM).

Next you can use reply_html_page/3 to deal with the navbar. Note that you can use HTML quasi quotations to include verbatim HTML into the Prolog source and pass parameters to it.

Is that an option?

Sorry, but I didn’t get the idea. The extract above works fine. It generates the Prolog code from an html file.
My html file contents:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <header>
            <!-- some lines of code here -->
            <nav>
                <ul>
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                </ul>
            </nav>
        </header>
       
        <!-- Begin page content -->
        <div class="container">
        </div>
    </body>
</html>

So include_html_file transforms the html code into Prolog. But how can I replace <nav> having DOM variable?

You could do so using normal Prolog term rewrite. The DOM is documented with load_structure/3.

I would generate dynamic parts of the page from Prolog though. That is way easier. Normally I’d use reply_html_page/3 and than define user:body//2 to separate the page header, footer, etc from the content. See e.g. https://github.com/SWI-Prolog/plweb/blob/a1300df10b1ef291d85f1757bc97b659f7a09417/page.pl for how this is done on the SWI-Prolog web site.

It seems like It would be better to write the logic like you mentioned. Here is one thought I didn’t catch: what’s user:body for?

The docs of reply_html_page/3 explain the idea. user:body/2 allows wrapping the content that you create from the page generation. This allows adding headers, footers, scripts, etc. The Style argument is available to the hook, so it can use different wrappers for different parts of the website.