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

Render a page without Diet in WebInterface ? #1480

Open
SkyzohKey opened this issue Apr 21, 2016 · 2 comments
Open

Render a page without Diet in WebInterface ? #1480

SkyzohKey opened this issue Apr 21, 2016 · 2 comments

Comments

@SkyzohKey
Copy link

SkyzohKey commented Apr 21, 2016

Hi dudes,
Before exposing my problem, I want to give what you deserve to: Thanks for this awesome work!

My "issue" here is that i would like to not use Diet templates. I imported mustache-d successfully, but now I don't know how I can use the result of Mustache.render and display it on my page. It seems that HTTPServerResponse.writeBody() would do the job but i don't understand how I can get a copy of the res object while being inside a WebInterface.

Here's my current code:

class WebInterface {
  private void renderTemplate(string file, Mustache.Context ctx)(HTTPServerResponse res) {
    Mustache mustache;
    mustache.path = "../views/";

    auto html = mustache.render(file, ctx);
    res.writeBody(html, 200, "text/html, charset=UTF-8");
  }

  @method(HTTPMethod.GET) @path("/")
  void index() {
    auto context = new Mustache.Context;
    res.renderTemplate("index", context);
  }
}

So, the error dmd is telling me is:

source/app.d(10,3): Error: undefined identifier 'res'

Thanks for the future replies ;)

@SkyzohKey SkyzohKey changed the title How to writeBody inside a WebInterface ? Render a page without Diet in WebInterface ? Apr 21, 2016
@s-ludwig
Copy link
Member

You can define a parameter of type HTTPServerResponse (or also HTTPServerRequest) and it will automatically get the current request:

  @method(HTTPMethod.GET) @path("/")
  void index(HTTPServerResponse res) {
    auto context = new Mustache.Context;
    res.renderTemplate("index", context);
  }

@BenjaminSchaaf
Copy link
Contributor

With #1938 (for issue #1937), you can access the global response object. That would allow you to write a render function similar to the one in vibe.web.web.

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

3 participants