1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.views.viewformats;
7 
8 import diamond.core.apptype;
9 
10 static if (isWebServer)
11 {
12   /// The format for view classes when using web-servers.
13   enum viewClassFormat = q{
14     final class view_%s : View
15     {
16       import std.array : replace, split, array, join, split;
17       import std.algorithm : map, filter, countUntil, count, canFind, startsWith, endsWith, splitter;
18       import std..string : strip, stripLeft, stripRight, indexOf, lastIndexOf, isNumeric, format;
19       import std.datetime : Date, DateTime, SysTime, Clock, Month, DayOfWeek;
20       import std.conv : to;
21 
22       import diamond.http;
23       import diamond.errors.exceptions;
24       import diamond.controllers;
25       import diamond.markdown;
26       import diamond.core.meta;
27       import i18n = diamond.data.i18n;
28 
29       import controllers;
30       import models;
31 
32       public:
33       final:
34       // viewClassMembers
35       %s
36 
37       this(HttpClient client, string name)
38       {
39         super(client, name);
40 
41         // viewConstructor
42         %s
43       }
44 
45       // modelGenerate
46       %s
47 
48       override string generate(string sectionName = "")
49       {
50         try
51         {
52           // controller
53           %s
54 
55           // placeholders
56           %s
57 
58           switch (sectionName)
59           {
60             // view code
61             %s
62           }
63 
64           // end
65           %s
66         }
67         catch (Exception e)
68         {
69           import std.file : write;
70           write("ERRORS.txt", e.toString);
71 
72           throw new ViewException(super.name, e);
73         }
74         catch (Throwable t)
75         {
76           import std.file : write;
77           write("ERRORS.txt", t.toString);
78 
79           throw new ViewError(super.name, t);
80         }
81       }
82     }
83   };
84 
85   /// The format for controller handlers.
86   enum controllerHandleFormat = q{
87     if (!super.rawGenerate)
88     {
89       auto controllerResult = controller.handle();
90 
91       if (controllerResult == Status.notFound)
92       {
93         client.notFound();
94       }
95       else if (controllerResult == Status.unauthorized)
96       {
97         client.unauthorized();
98       }
99       else if (controllerResult == Status.end)
100       {
101         return null;
102       }
103     }
104   };
105 
106   /// The format for the controller member.
107   enum controllerMemberFormat = "%s!%s controller;\r\n";
108 
109   /// The format for controller constructors.
110   enum controllerConstructorFormat = "controller = new %s!%s(this);\r\n";
111 
112 }
113 else
114 {
115   /// The format for view classes when using stand-alone.
116   enum viewClassFormat = q{
117     final class view_%s : View
118     {
119       import std.array : replace, split, array, join, split;
120       import std.algorithm : map, filter, countUntil, count, canFind, startsWith, endsWith, splitter;
121       import std..string : strip, stripLeft, stripRight, indexOf, lastIndexOf, isNumeric, format;
122       import std.datetime : Date, DateTime, SysTime, Clock, Month, DayOfWeek;
123       import std.conv : to;
124 
125       import diamond.errors.exceptions;
126       import diamond.core.meta;
127 
128       import models;
129 
130       public:
131       final:
132       // viewClassMembers
133       %s
134 
135       this(string name)
136       {
137         super(name);
138 
139         // viewConstructor
140         %s
141       }
142 
143       // modelGenerate
144       %s
145 
146       override string generate(string sectionName = "")
147       {
148         try
149         {
150           // placeholders
151           %s
152 
153           switch (sectionName)
154           {
155             // view code
156             %s
157           }
158 
159           // end
160           %s
161         }
162         catch (Exception e)
163         {
164           throw new ViewException(super.name, e);
165         }
166         catch (Throwable t)
167         {
168           throw new ViewError(super.name, t);
169         }
170       }
171     }
172   };
173 }
174 
175 static if (isWebServer || !isWeb)
176 {
177   /// The format for generating the view using a model.
178   enum modelGenerateFormat = q{
179     string generateModel(%s newModel, string sectionName = "")
180     {
181       model = newModel;
182 
183       return generate(sectionName);
184     }
185   };
186 
187   /// The format for the model member.
188   enum modelMemberFormat = "%s model;\r\n";
189 
190   enum updateModelFromRenderViewFormat = q{
191     protected override void copyViewData()
192     {
193       auto rView = cast(view_%s)renderView;
194 
195       if (rView)
196       {
197         this.model = rView.model;
198       }
199 
200       super.copyViewData();
201     }
202   };
203 
204   /// The format for ending a view generate call.
205   enum endFormat = q{
206     if (sectionName && sectionName.length)
207     {
208       return generate(null);
209     }
210     else
211     {
212       return super.prepare();
213     }
214   };
215 
216   /// The format for place holders.
217   enum placeholderFormat = q{
218     foreach (key,value; %s)
219     {
220       super.addPlaceholder(key,value);
221     }
222   };
223 
224   /// The format for appended arguments.
225   enum appendFormat = "append(%s);\r\n";
226 
227   /// The format for escaped arguments.
228   enum escapedFormat = "escape(%s);\r\n";
229 
230   /// The format used for layout's in the constructor.
231   enum layoutConstructorFormat = "super.layoutName = \"%s\";\r\n";
232 }