1 module templ;
2 
3 
4 import vibe.core.log;
5 
6 import std.conv;
7 import std.functional;
8 
9 import vayne.compiler;
10 import vayne.lib;
11 import vayne.serializer;
12 import vayne.vm;
13 
14 
15 void render(OutputStreamT, string FileName, Vars...)(OutputStreamT o__, string language__) {
16 	alias VayneVM = VM!();
17 	VayneVM vm;
18 
19 	auto compiled = unserialize(cast(ubyte[])std.file.read("views/" ~ FileName ~ ".vayne"));
20 
21 	Value[] constants;
22 	constants.reserve(compiled.constants.length);
23 
24 	foreach(i, c; compiled.constants) {
25 		final switch (c.type) with (ConstantType) {
26 		case Null:
27 			constants ~= Value(null);
28 			break;
29 		case Boolean:
30 			constants ~= Value(c.value.to!bool);
31 			break;
32 		case Integer:
33 			constants ~= Value(c.value.to!long);
34 			break;
35 		case Float:
36 			constants ~= Value(c.value.to!double);
37 			break;
38 		case String:
39 			constants ~= Value(c.value.to!string);
40 			break;
41 		}
42 	}
43 
44 	vm.load(compiled.registerCount, constants, compiled.instrs, compiled.locs, compiled.sources);
45 
46 	VayneVM.Globals globals;
47 
48 	bindLibDefault(globals);
49 
50 	mixin(bindVars!(0, globals, Vars));
51 
52 	auto translate(Value[] args) {
53 		assert(language__ == "en");
54 		auto tag = args[0].get!string;
55 		switch (tag) {
56 			case "footer":	return "This is the footer translation";
57 			case "empty":	return "Move along, nothing to see here";
58 			default: return tag;
59 		}
60 	}
61 
62 	static errorHandler(VayneVM.Error error) {
63 		logInfo("%s(%s) template error: %s", error.source, error.line, error.msg);
64 	}
65 
66 	globals["__translate"] = Value(&translate);
67 	vm.bindGlobals(globals);
68 
69 	vm.errorHandler = toDelegate(&errorHandler);
70 	vm.execute(o__);
71 }