Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sending HTML best practice #125

Open
jschmitt-mscs opened this issue Sep 2, 2020 · 2 comments
Open

sending HTML best practice #125

jschmitt-mscs opened this issue Sep 2, 2020 · 2 comments

Comments

@jschmitt-mscs
Copy link

Is the best way to send HTML via Restinio as a file or written out as in the examples?
The below is working for me. What would be the best way to pass the parameter "test" and to make an SQL query and have the data returned into the browser?

router->http_get("/index/:test",
    [](auto req, auto params) {
        return
            init_resp(req->create_response())
            .append_header(restinio::http_field::content_type, "text/html; charset=utf-8")
            .set_body(restinio::sendfile("C\\:..\html1.htm"))
            .done();
    });
@eao197
Copy link
Member

eao197 commented Sep 2, 2020

Hi!

I think "the best way" depends on your case. If you have to return just static HTML content then sendfile would be a better choice. But if you have to integrate some dynamic data into your HTML then you have to use set_body or append_body methods of response builder (maybe with help of some HTML template generators like jinja2cpp (a list of C++ HTML template engines can be found here)).

An example of performing database requests during the request processing can be found here.

@jschmitt-mscs
Copy link
Author

Thanks for the help and recommendations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants