## HTML Controller

The `html_controller` class allows you to mount a request handler on a URI path, the result is that this request handler, which is a method of your controller class, will be called whenever a HTTP request with a matching URI comes in.

The handler method has next to the `request` and `reply` parameter an additional `scope` parameter. This scope is a kind of nested map of variable names and values. The scope is `const`, if you want to add data to the scope you should create your own sub scope and pass the original in the constructor.

A handler can of course create simple replies, just as in the previous example. But you can also use templates. Note that the constructor of `html_controller` takes a second parameter that is called docroot. This should contain the path to the directory containing the templates.

| Note
The docroot parameter is ignored when you create a html controller based on resources, see section on resources further in this documentation.

Our `html_controller` indirectly inherits `template_processor` and this is the class that uses the `docroot` parameter. This class takes care of processing template files. It loads them and uses the registered tag processors and the `scope` to fill in the blanks and process the constructs found in the template.

    #include <zeep/http/server.hpp>
    #include <zeep/http/html-controller.hpp>
    #include <zeep/http/template-processor.hpp>

    class hello_controller : public zeep::http::html_controller
    {
      public:
        hello_controller()
        {
            mount("{,index,index.html}", &hello_controller::handle_index);
        }

        void handle_index(const zeep::http::request& req, const zeep::http::scope& scope, zeep::http::reply& rep)
        {
            zeep::http::scope sub(scope);
            auto name = req.get_parameter("name");
            if (not name.empty())
                sub.put("name", name);

            get_template_processor().create_reply_from_template("hello.xhtml", sub, rep);
        }
    };

    int main()
    {
        zeep::http::server srv("docroot");

        srv.add_controller(new hello_controller());

        srv.bind("::", 8080);
        srv.run(2);

        return 0;
    }

| Mount the handler `handle_index` on `/`, `/index` and `/index.html`
| Use the server constructor that takes the path to a docroot so it will construct a template processor

This example uses the file docroot/hello.xhtml which should contain:

    <!DOCTYPE html SYSTEM "about:legacy-compat">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:z="http://www.hekkelman.com/libzeep/m2">
      <head>
        <title>Hello</title>
      </head>
      <p>Hello, <span z:text="${name ?: 'world'}"/>!</p>
    </html>

Now build and run this code, and you can access your welcome page at http://localhost:8080/. If you want to see another name, use e.g. http://localhost:8080/?name=maarten instead.

Several remarks here.

The server object is created with a `docroot` parameter. That parameter tells the server to create a default `template_processor` for use by the `html_controller` objects.

As you can see in the handler code, a check is made for a parameter called `name`. When present, its value is stored in the newly created sub-scope object. The template file contains a construct in the `<span>` element that tests for the availability of this variable and uses the default `'world'` otherwise. For more information on templates see the section on xhtml templates.

The path specified in `mount` is `{,index,index.html}` which is a glob pattern, this pattern can accept the following constructs:

Table 3.1. The accepted glob pattern constructs

path | matches
--- | ---
`**/*.js` | matches `x.js`, `a/b/c.js`, etc
`{css,scripts}/` | matches e.g. `css/1/first.css` and `scripts/index.js`
`a;b;c` | matches either `a`, `b` or `c`