HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素相关的知识,希望对你有一定的参考价值。
/*** 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
/** * 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 $contained_data = $propBag['_data']; //SAVE THE VALUE IN A VARIABLE $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 "_data"=>"Form Legend" )); //FORM TEXT INPUT AND LABEL //may also be used for checkbox,radio,and textarea "for"=>"function_test", "_data"=>"html function test" )); "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 //HYPER_LINK "id"=>"myanchor", "href"=>"http://www.google.com", "_data"=>"link text" )); //HYPERLINK WITH AN IMAGE AS CONTAINED DATA "class"=>"lightbox", "href"=>$filename, "src"=>$filename, "class"=>"thumbnail img-polaroid span2", "alt"=>"" )) )); //META TAG 'name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv' ));
以上是关于HTML助手函数创建具有任意多个属性(self-closing和container)的任意元素的主要内容,如果未能解决你的问题,请参考以下文章