1 /** 2 * Skadi.d Web Framework 3 * Validation Component 4 5 * Authors: Faianca 6 * Copyright: Copyright (c) 2015 Faianca 7 * License: MIT License, see LICENSE 8 */ 9 module skadi.components.jade.builder; 10 11 import std.stdio; 12 import std.traits; 13 import std.typetuple; 14 15 /** 16 * Html attributes in order to be used. 17 **/ 18 struct HTMLAttributes 19 { 20 string rel; 21 string type; 22 string src; 23 string href; 24 string action; 25 string id; 26 string classes; 27 string forAttr; 28 string name; 29 string value; 30 string style; 31 } 32 33 34 class Builder 35 { 36 37 static: 38 39 /** 40 * Renders parameters keeping order in their HTML attributes 41 */ 42 string renderAttributes(string code, HTMLAttributes attributes) 43 { 44 foreach (string field; TypeTuple!(__traits(derivedMembers, HTMLAttributes))) { 45 if(string s = __traits(getMember, attributes, field) ) { 46 47 string fieldAttr; 48 49 // Hack for the class keyword 50 if (field == "classes") { 51 fieldAttr = "class"; 52 } else { 53 fieldAttr = field; 54 } 55 56 code ~= " " ~ fieldAttr ~ "=\"" ~ s ~ "\""; 57 } 58 } 59 60 return code; 61 } 62 } 63 64 unittest 65 { 66 auto attr = HTMLAttributes(); 67 attr.value = "lol"; 68 attr.type = "text"; 69 attr.classes = "super-class test2"; 70 attr.style = "display:none;"; 71 72 string jade = Builder.renderAttributes(".input", attr); 73 assert(jade == ".input type=\"text\" class=\"super-class test2\" value=\"lol\" style=\"display:none;\""); 74 }