ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

bougayez opened this issue · comments

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

  • I am sure I am reading the files correctly.
    Here is the full code :
#include <boost/filesystem.hpp>
#include "include/crow_all.h"
#include <string>
using namespace crow;
using namespace crow::mustache;
using namespace std;

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 :

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="https://github.com/ipkn/crow/issues/styles/style.css" target="_blank" rel="nofollow">
  </head>
  <body>
    <h1>Animals (Crow)</h1>
    {{#animals}}
    <a href="static/{{image}}">
        <div class="divCss">
          <p> {{name}} </p>
          <img class="imgCss" src="static/{{image}}" />
        </div>
      </a>
    {{/animals}}
    <p style="clear: both"><a href="/about">About</a></p>
  </body>
</html>

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

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