<% '======================================================================================================================= ' HTML HELPER '======================================================================================================================= Class HTML_Helper_Class 'Duplicate of Routes.NoCacheToken, copied to avoid extra lookup into the Routes object for such a trivial function. 'Allows caller to reference HTML.NoCacheToken in cases where it seems to feel right. Public Property Get NoCacheToken NoCacheToken = Timer() * 100 End Property 'Ensures safe output Public Function Encode(ByVal value) If Not IsEmpty(value) and Not IsNull(value) then Encode = Server.HtmlEncode(value) End If End Function '--------------------------------------------------------------------------------------------------------------------- 'LinkTo and its relatives DO NOT HTMLEncode the link_text! This allows use of HTML within the link, especially 'useful for Bootstrap icons and the like. ' 'Bottom Line: If you need to HTMLEncode the link text YOU MUST DO IT YOURSELF! The H() method makes this easy! Public Function LinkTo(link_text, controller_name, action_name) LinkTo = LinkToExt(link_text, controller_name, action_name, empty, empty) End Function Public Function LinkToExt(link_text, controller_name, action_name, params_array, attribs_array) LinkToExt = "" & link_text & "" & vbCR End Function Public Function LinkToIf(condition, link_text, controller_name, action_name) if condition then LinkToIf = LinkToExt(link_text, controller_name, action_name, empty, empty) end if End Function Public Function LinkToExtIf(condition, link_text, controller_name, action_name, params_array, attribs_array) if condition then LinkToExtIf = LinkToExt(link_text, controller_name, action_name, params_array, attribs_array) end if End Function Public Function LinkToUnless(condition, link_text, controller_name, action_name) if not condition then LinkToIf = LinkToExt(link_text, controller_name, action_name, empty, empty) end if End Function Public Function LinkToExtUnless(condition, link_text, controller_name, action_name, params_array, attribs_array) if not condition then LinkToExtUnless = LinkToExt(link_text, controller_name, action_name, params_array, attribs_array) end if End Function '' ' Creates a form button and a hidden form to enforce POST submissions. Params are in hidden fields. '' 'Public Function PostButtonLinkTo(controller_name, action_name, params) ' dim id : id = "post_button__" & controller_name & action_name ' dim s ' s = "
" ' dim i, key, val ' for i = 0 to ubound(params) step 2 ' KeyVal params, i, key, val ' s = s & "" ' next ' s = s & "" ' s = s & "
" ' PostButtonLinkTo = s 'End Function Public Function PostButtonTo(button_contents, controller_name, action_name, form_fields) PostButtonTo = PostButtonToExt(button_contents, controller_name, action_name, form_fields, empty) End Function Public Function PostButtonToExt(button_contents, controller_name, action_name, form_fields, url_params) dim s : s = "
" dim i, key, val for i = 0 to ubound(form_fields) step 2 KeyVal form_fields, i, key, val s = s & HTML.Hidden(key, val) next s = s & HTML.SubmitButton(button_contents) s = s & "
" & vbCR PostButtonToExt = s End Function Public Function AppStylesheetTag AppStylesheetTag = StylesheetTag(Routes.StylesheetsURL & "App.css") End Function Public Function ControllerStylesheetTag ControllerStylesheetTag = StylesheetTag(Routes.StylesheetsUrl & MVC.ControllerName & "Controller.css") End Function Public Function StylesheetTag(url) StylesheetTag = "" & vbCR End Function Public Function JSTag(url) JSTag = "" & vbCR End Function '--------------------------------------------------------------------------------------------------------------------- ' Form Helpers '--------------------------------------------------------------------------------------------------------------------- Public Function FormTag(controller_name, action_name, route_attribs, form_attribs) FormTag = "
" & vbCR End Function Public Function Label(name, for_name) Label = LabelExt(name, for_name, empty) End Function Public Function LabelExt(name, for_name, attribs) LabelExt = "" & vbCR End Function Public Function Hidden(id, value) Hidden = HiddenExt(id, value, empty) End Function Public Function HiddenExt(id, value, attribs) HiddenExt = "" & vbCR End Function Public Function TextBox(id, value) TextBox = TextBoxExt(id, value, empty) End Function Public Function TextBoxExt(id, value, attribs) TextBoxExt = "" & vbCR End Function Public Function TextArea(id, value, rows, cols) TextArea = TextAreaExt(id, value, rows, cols, empty) End Function Public Function TextAreaExt(id, value, rows, cols, attribs) TextAreaExt = "" & vbCR End Function '--------------------------------------------------------------------------------------------------------------------- 'If list is a recordset then option_value_field and option_text_field are required. 'If list is an array the method assumes it is a KVArray and those parameters are ignored. Public Function DropDownList(id, selected_value, list, option_value_field, option_text_field) DropDownList = DropDownListExt(id, selected_value, list, option_value_field, option_text_field, empty) End Function Public Function DropDownListExt(id, selected_value, list, option_value_field, option_text_field, attribs) If IsNull(selected_value) then selected_value = "" Else selected_value = CStr(selected_value) End If dim item, options, opt_val, opt_txt options = "" & vbCR End If list.MoveNext loop case "Variant()" 'assumes KVArray dim i for i = 0 to ubound(list) step 2 KeyVal list, i, opt_val, opt_txt options = options & "" & vbCR next end select DropDownListExt = "" & vbCR End Function Public Function Checkbox(id, value) Checkbox = CheckboxExt(id, value, empty) End Function Public Function CheckboxExt(id, value, attribs) CheckBoxExt = "" & vbCR End Function '--------------------------------------------------------------------------------------------------------------------- 'Button text IS NOT ENCODED! As with LinkTo, this allows use of Bootstrap icons and other arbitrary HTML in the 'button. If you need to HTMLEncode the text you MUST do it yourself! Public Function SubmitButton(text) SubmitButton = "" & vbCR End Function Public Function Button(button_type, text, class_name) Button = "" & vbCR End Function Public Function ButtonExt(button_type, text, attribs_array) ButtonExt = "" & vbCR End Function '--------------------------------------------------------------------------------------------------------------------- Public Function Tag(Tag_name, attribs_array) Tag = "<" & Encode(tag_name) & " " & HtmlAttribs(attribs_array) & ">" End Function Public Function Tag_(Tag_name) Tag_ = "" End Function '--------------------------------------------------------------------------------------------------------------------- Public Function HtmlAttribs(attribs) dim result : result = "" if not IsEmpty(attribs) then if IsArray(attribs) then dim idx for idx = lbound(attribs) to ubound(attribs) step 2 result = result & " " & HtmlAttrib(attribs, idx) & " " next else ' assume string or string-like default value result = attribs end if end if HtmlAttribs = result End Function Public Function HtmlAttrib(attribs_array, key_idx) dim key, val KeyVal attribs_array, key_idx, key, val HtmlAttrib = Encode(key) & "='" & Encode(val) & "'" End Function End Class dim HTML_Helper__Singleton : set HTML_Helper__Singleton = Nothing Function HTML() if HTML_Helper__Singleton Is Nothing then set HTML_Helper__Singleton = new HTML_Helper_Class End if set HTML = HTML_Helper__Singleton End Function %>