1 module vibeauth.mvc.view;
2 
3 import std.string;
4 import std.algorithm;
5 
6 import vibeauth.mvc.templatedata;
7 import vibeauth.configuration;
8 
9 import vibe.data.json;
10 
11 /// Default view
12 class View {
13   protected {
14     const string stringTemplate;
15   }
16 
17   /// Data that will be used in the template
18   TemplateData data;
19 
20   ///
21   alias data this;
22 
23   /// Create the template
24   this(const string stringTemplate) {
25     this.stringTemplate = stringTemplate;
26   }
27 
28   /// ditto
29   this(const string stringTemplate, Json defaultOptions) {
30     this.stringTemplate = stringTemplate;
31     data.add(defaultOptions);
32   }
33 
34   /// Generates an empty body. If #{body} variable is present, it will be removed
35   string generateBody() {
36     return "";
37   }
38 
39   /// Replace all the variables with the provided options
40   string render() {
41     auto result = data.render(stringTemplate.replace("#{body}", generateBody()));
42 
43     int count;
44     while(count < 5 && result.canFind("#{")) {
45       count++;
46       result = data.render(result);
47     }
48 
49     return result;
50   }
51 }
52 
53 /// View that will use a configured templates
54 class BasicView(string tplProperty, string bodyProperty) : View {
55   private {
56     const ServiceConfiguration configuration;
57   }
58 
59   ///
60   this(const ServiceConfiguration configuration) {
61     this.configuration = configuration;
62 
63     mixin(`super(configuration.` ~ tplProperty ~ `, configuration.serializeToJson);`);
64   }
65 
66   ///
67   override string generateBody() {
68     mixin(`return configuration.` ~ bodyProperty ~ `;`);
69   }
70 }