# How to pass variables to html?

**URL:** <https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617>\
**Category:** Help!\
**Created:** [February 18, 2021, 3:07am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617 "2021-02-18T03:07:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![evmusonov](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/evmusonov/32/2967_2.png) [@evmusonov](https://swi-prolog.discourse.group/u/evmusonov)\
**Post date:** [February 18, 2021, 3:07am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/1 "2021-02-18T03:07:11Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 18, 2021, 7:57am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/2 "2021-02-18T07:57:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![evmusonov](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/evmusonov/32/2967_2.png) [@evmusonov](https://swi-prolog.discourse.group/u/evmusonov)\
**Post date:** [February 18, 2021, 8:56am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/3 "2021-02-18T08:56:01Z")

</div>

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

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

```

For example, I can do:

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

```

Although I wonder how to integrate `navbar` into `editor`?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 18, 2021, 10:02am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/4 "2021-02-18T10:02:14Z")

</div>

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

```prolog
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?

---

<div class="post-metadata">

**Author:** ![evmusonov](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/evmusonov/32/2967_2.png) [@evmusonov](https://swi-prolog.discourse.group/u/evmusonov)\
**Post date:** [February 18, 2021, 12:26pm UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/5 "2021-02-18T12:26:37Z")

</div>

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:

```prolog
<!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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 18, 2021, 12:59pm UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/6 "2021-02-18T12:59:49Z")

</div>

> [@evmusonov](#):
>
> 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](https://github.com/SWI-Prolog/plweb/blob/a1300df10b1ef291d85f1757bc97b659f7a09417/page.pl) for how this is done on the SWI-Prolog web site.

---

<div class="post-metadata">

**Author:** ![evmusonov](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/evmusonov/32/2967_2.png) [@evmusonov](https://swi-prolog.discourse.group/u/evmusonov)\
**Post date:** [February 20, 2021, 9:12am UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/7 "2021-02-20T09:12:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 20, 2021, 2:09pm UTC](https://swi-prolog.discourse.group/t/how-to-pass-variables-to-html/3617/8 "2021-02-20T14:09:14Z")

</div>

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.
