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.viewroute; 7 8 import diamond.core.apptype; 9 10 static if (isWebServer || !isWeb) 11 { 12 /// Mixin template for generating the getView() function. 13 mixin template GenerateGetView() 14 { 15 import diamond.core.apptype; 16 import std..string : format; 17 import diamondapp; 18 19 static if (isWebServer) 20 { 21 import diamond.http; 22 23 /** 24 * Generates the getView() function. 25 * Returns: 26 * The resulting string of the getView() function to use in a mixin. 27 */ 28 string generateGetView() 29 { 30 string getViewMixin = " 31 View getView(HttpClient client, Route route, bool checkRoute, bool keepRoute = false) 32 { 33 auto viewName = 34 routableViews.get(route.name, checkRoute ? null : route.name); 35 36 if (!viewName) 37 { 38 import diamond.http.routing; 39 viewName = getViewNameFromRoute(route.name); 40 } 41 42 if (!viewName) 43 { 44 return null; 45 } 46 47 switch (viewName) 48 { 49 "; 50 51 mixin LoadViewData!true; 52 53 foreach (viewName; getViewData().keys) 54 { 55 getViewMixin ~= format(q{ 56 case "%s": 57 { 58 if (!keepRoute) 59 { 60 client.route = route; 61 } 62 63 return new view_%s(client, "%s"); 64 } 65 }, viewName, viewName, viewName); 66 } 67 68 getViewMixin ~= " 69 default: return null; // 404 ... 70 } 71 } 72 "; 73 74 return getViewMixin; 75 } 76 } 77 else 78 { 79 /** 80 * Generates the getView() function. 81 * Returns: 82 * The resulting string of the getView() function to use in a mixin. 83 */ 84 string generateGetView() 85 { 86 string getViewMixin = " 87 View getView(string viewName) 88 { 89 if (!viewName || !viewName.length) 90 { 91 return null; 92 } 93 94 switch (viewName) 95 { 96 "; 97 98 mixin LoadViewData!true; 99 100 foreach (viewName; getViewData().keys) 101 { 102 getViewMixin ~= format(q{ 103 case "%s": 104 { 105 return new view_%s("%s"); 106 } 107 }, viewName, viewName, viewName); 108 } 109 110 getViewMixin ~= " 111 default: return null; // 404 ... 112 } 113 } 114 "; 115 116 return getViewMixin; 117 } 118 } 119 120 mixin(generateGetView); 121 } 122 }