Revision: 60996
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at November 21, 2012 08:19 by halk
Initial Code
/** * THIS FUNCTION WILL RENDER A CONTAINER OR SELF-CLOSING HTML ELEMENT. * IT AUTO DETECTS WHAT TYPE YOU WANT * BASED ON RATHER OR NOT YOU SET A _data KEY IN THE ARRAY OF * ATTRIBUTES PASSED TO THE FUNCTION * * USING AN ASSOCIATIVE ARRAY ATTRIBUTES ARE ASSIGNED TO THE ELEMENT * * IF THE KEY _data IS SET IN THE $propBag ARRAY * _data WILL BE INSERTED BETWEEN THE START AND END TAGS * AS THE ELEMENT IS NOT SELF CLOSING BECAUSE IT HAS DATA TO CONTAIN. * * IF _data IS NOT SENT IN THE ARRAY THE TAG WILL BE FORMED AS SELF CLOSING * BECAUSE IT HAS NO DATA TO CONTAIN */ /** * html() * * @param mixed $elm * @param mixed $propBag * @return */ function html($elm,$propBag){ $self_closing=false; $attributes=""; $contained_data=""; //THE VALUE OF THE '_data' ARRAY ELEMENT IN $propBag if(array_key_exists("_data",$propBag)){ //IF _data EXISTS IN $propBag $contained_data = $propBag['_data']; //SAVE THE VALUE IN A VARIABLE unset($propBag['_data']); //REMOVE contained_data FROM THE ARRAY $self_closing=false; //indicate this is not a self closing tag because we have data to contain }else{ //IF contained_data IS NOT A KEY IN THE ARRAY $contained_data = NULL;//SET THE $contained_data VARIAB;E TO NULL $self_closing=true; //indicate it is a self-closing tag that contains no data } $start_tag = "<".$elm; foreach($propBag as $key => $value){ $attributes .= " $key=\"$value\""; } if(!$self_closing){ $start_tag .= " ".$attributes.">";//CLOSE THE START_TAG AFTER SETTING ALL THE ATTRIBUTES FOR THE ELEMENT $end_tag = "</".$elm.">"; //BUILD THE CLOSING TAG $html = $start_tag.$contained_data.$end_tag; //WRAP THE CONTAINED DATA IN THE START AND END TAGS }else{ $start_tag .= " ".$attributes; $end_tag = '/>'; $html = $start_tag.$end_tag; } return $html; } //EXAMPLES //FORM LEGEND echo html("legend",array( "_data"=>"Form Legend" )); //FORM TEXT INPUT AND LABEL //may also be used for checkbox,radio,and textarea echo html("label",array( "for"=>"function_test", "_data"=>"html function test" )); echo html("input",array( "name"=>"function_test", "type"=>"text", "class"=>"input-xxlarge", "placeholder"=>"i guess it worked", //CUSTOM ATTRIBUTE "rel"=>"tooltip", "title"=>"this is my tooltip", "style"=>"border:10px solid red;", "onchange"=>"alert('boo hoo');" //EXECUTE JS TRIGGERED BY EVENT )); //IMAGE TAG echo html("img",array("alt"=>"","src"=>"images/logo.png","height"=>"50")); //HYPER_LINK echo html("a",array( "id"=>"myanchor", "href"=>"http://www.google.com", "_data"=>"link text" )); //HYPERLINK WITH AN IMAGE AS CONTAINED DATA $images .= html("a",array( "class"=>"lightbox", "href"=>$filename, "_data"=> html("img",array( "src"=>$filename, "class"=>"thumbnail img-polaroid span2", "alt"=>"" )) )); //META TAG echo html("meta",array( 'name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv' ));
Initial URL
Initial Description
/** * THIS FUNCTION WILL RENDER A CONTAINER OR SELF-CLOSING HTML ELEMENT. * IT AUTO DETECTS WHAT TYPE YOU WANT * BASED ON RATHER OR NOT YOU SET A _data KEY IN THE ARRAY OF * ATTRIBUTES PASSED TO THE FUNCTION * * USING AN ASSOCIATIVE ARRAY ATTRIBUTES ARE ASSIGNED TO THE ELEMENT * * IF THE KEY _data IS SET IN THE $propBag ARRAY * _data WILL BE INSERTED BETWEEN THE START AND END TAGS * AS THE ELEMENT IS NOT SELF CLOSING BECAUSE IT HAS DATA TO CONTAIN. * * IF _data IS NOT SENT IN THE ARRAY THE TAG WILL BE FORMED AS SELF CLOSING * BECAUSE IT HAS NO DATA TO CONTAIN */ SEE Examples Of Use Below The Code
Initial Title
HTML Helper Function Create Any Element With Any Number Of Attributes (self-closing and container tags)
Initial Tags
php, html
Initial Language
PHP