Skip to content

Commit

Permalink
Merge pull request #26 from Yoplitein/fixContextErrors
Browse files Browse the repository at this point in the history
Fix compile errors when using TempleContext with Vibe
  • Loading branch information
dymk committed Feb 19, 2015
2 parents 9321852 + 08223cd commit 66def3e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/temple/tests/vibe.d
Expand Up @@ -14,14 +14,13 @@ private {
/*
* Drops HTTP headers from the stream output and uses proper newlines
*/
private string rendered(MemoryOutputStream output)
{
private string rendered(MemoryOutputStream output) {
import std.string: split, join;

string data = cast(string)output.data;
string[] lines = data.split("\r\n");
lines = lines[4 .. $];

return lines.join("\n");
}

Expand All @@ -42,6 +41,24 @@ unittest {
`));
}

unittest {
auto output = new MemoryOutputStream();
auto resp = createTestHTTPServerResponse(output);
auto ctx = new TempleContext;
ctx.abc = "<unescaped>";
ctx.def = "<escaped>";
resp.renderTemple!`
<%= safe(var.abc) %>
<%= var.def %>
`(ctx);
resp.bodyWriter.flush; //flushes resp's output stream wrapping the MemoryOutputStream

assert(isSameRender(output.rendered, `
<unescaped>
&lt;escaped&gt;
`));
}

unittest {
auto output = new MemoryOutputStream();
auto resp = createTestHTTPServerResponse(output);
Expand Down
9 changes: 9 additions & 0 deletions src/temple/vibe.d
Expand Up @@ -9,6 +9,7 @@ private {
import vibe.http.server;
import vibe.textfilter.html;
import std.stdio;
import std.variant;
}

struct TempleHtmlFilter {
Expand All @@ -21,13 +22,21 @@ struct TempleHtmlFilter {
filterHTMLEscape(stream, unsafe);
}

static void temple_filter(ref TempleOutputStream stream, Variant variant) {
temple_filter(stream, variant.toString);
}

static string temple_filter(SafeString safe) {
return safe.payload;
}

static SafeString safe(string str) {
return SafeString(str);
}

static SafeString safe(Variant variant) {
return SafeString(variant.toString);
}
}

private enum SetupContext = q{
Expand Down

0 comments on commit 66def3e

Please sign in to comment.