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