ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.47k stars 889 forks source link

"crow::mustache::load("../home.html").render(context)" generates a blank page #386

Closed bougayez closed 3 years ago

bougayez commented 3 years ago

When I try to render an HTML page with crow::mustache it gives a blank page!

int main() { crow::SimpleApp app; CROW_ROUTE(app, "/") ([]{ crow::mustache::context ctx; ctx["animals"][0]["name"] = "chouette"; ctx["animals"][0]["image"] = "owl.jpg"; ctx["animals"][1]["name"] = "owl"; ctx["animals"][1]["image"] = "owl.jpg"; return crow::mustache::load("../home.html").render(ctx); });

CROW_ROUTE(app, "/about")
  ([]{
   return crow::mustache::load("../about.html").render();
   });
app.port(3000).multithreaded().run();

}

The HTML page : 

Animals (Crow)

{{#animals}}

{{name}}

{{/animals}}

About

The-EDev commented 3 years ago

I ran the same code you provided, I believe the problem you have is that you're referencing the wrong directory.

Your templates (html files) need to be in a templates folder, then you should be able to reference them directly, so crow::mustache::load("../home.html").render(ctx) would actually try to find templates/../home.html.

I ran your code with and without ../, the instance with it, I had the html file next to my source file. In the instance without, I had it in templates/home.html, this produced a page with everything but the images (i used a placeholder for owl.jpg).

The images issue is caused due to the fact that your browser is asking the crow server for the image files, which it cannot find since there's no route for it. the Crow version here does support static file serving, but we don't have a default static/ folder yet. For that you'll need a specific /static/<string> route which sends the static file as defined here.

Please not that the crow version linked above requires boost 1.7 as opposed to boost 1.55 required here.

Feel free to let me know about anything else you need, I'm happy to help :D

bougayez commented 3 years ago

Hi, Thank you for your reply. That was a life saver. I put the HTML files in a templates and it worked just fine.