1 module scorpion.view;
2 
3 import std.array : Appender;
4 import std..string : split;
5 
6 import diet.html : compileHTMLDietFile;
7 
8 import lighttp.util : ServerRequest, ServerResponse;
9 
10 import scorpion.lang : LanguageManager, Lang;
11 
12 struct View {
13 
14 	private ServerRequest _request;
15 	private ServerResponse _response;
16 	private Lang _lang;
17 
18 	this(ServerRequest request, ServerResponse response, LanguageManager languageManager) {
19 		_request = request;
20 		_response = response;
21 		_lang = languageManager.get(languageManager.defaultLanguage);
22 		auto ptr = "accept-language" in request.headers;
23 		if(ptr) {
24 			foreach(language ; split(*ptr, ",")) {
25 				if(language.length >= 2 && languageManager.has(language[0..2])) {
26 					_lang = languageManager.get(language[0..2]);
27 					break;
28 				}
29 			}
30 		}
31 	}
32 
33 	@property ServerRequest request() {
34 		return _request;
35 	}
36 
37 	@property ServerResponse response() {
38 		return _response;
39 	}
40 
41 	@property Lang lang() {
42 		return _lang;
43 	}
44 
45 }
46 
47 void render(string file, E...)(View view) {
48 	view.response.body_ = renderImpl!(file, E)(view);
49 }
50 
51 string renderImpl(string file, E...)(View view) {
52 	Appender!string ret;
53 	ServerRequest request = view.request;
54 	Lang lang = view.lang;
55 	compileHTMLDietFile!(file, request, lang, E)(ret);
56 	return ret.data;
57 }