1 /* 2 * Hunt - A high-level D Programming Language Web framework that encourages rapid development and clean, pragmatic design. 3 * 4 * Copyright (C) 2015-2019, HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.framework.view.View; 13 14 import hunt.framework.view.Environment; 15 import hunt.framework.view.Template; 16 import hunt.framework.view.Util; 17 import hunt.framework.view.Exception; 18 import hunt.framework.Init; 19 20 // import hunt.framework.Simplify; 21 22 import hunt.serialization.JsonSerializer; 23 import hunt.logging; 24 25 import std.json : JSONValue; 26 import std.path; 27 28 class View { 29 private { 30 string _templatePath = "./views/"; 31 string _extName = ".html"; 32 uint _arrayDepth = 3; 33 Environment _env; 34 string _routeGroup = DEFAULT_ROUTE_GROUP; 35 JSONValue _context; 36 } 37 38 this(Environment env) { 39 _env = env; 40 } 41 42 Environment env() { 43 return _env; 44 } 45 46 View setTemplatePath(string path) { 47 _templatePath = path; 48 _env.setTemplatePath(path); 49 50 return this; 51 } 52 53 View setTemplateExt(string fileExt) { 54 _extName = fileExt; 55 return this; 56 } 57 58 string getTemplatePath() { 59 return _templatePath; 60 } 61 62 View setRouteGroup(string rg) { 63 _routeGroup = rg; 64 //if (_routeGroup != DEFAULT_ROUTE_GROUP) 65 _env.setRouteGroup(rg); 66 _env.setTemplatePath(buildNormalizedPath(_templatePath) ~ dirSeparator ~ _routeGroup); 67 68 return this; 69 } 70 71 View setLocale(string locale) { 72 _env.setLocale(locale); 73 return this; 74 } 75 76 int arrayDepth() { 77 return _arrayDepth; 78 } 79 80 View arrayDepth(int value) { 81 _arrayDepth = value; 82 return this; 83 } 84 85 string render(string tempalteFile) { 86 version (HUNT_VIEW_DEBUG) { 87 tracef("---tempalteFile: %s, _extName:%s, rend context: %s", 88 tempalteFile, _extName, _context.toString); 89 } 90 return _env.renderFile(tempalteFile ~ _extName, _context); 91 } 92 93 void assign(T)(string key, T t) { 94 this.assign(key, toJson(t)); 95 } 96 97 void assign(string key, JSONValue t) { 98 _context[key] = t; 99 } 100 } 101