Why use DOMDocument instead of PHP with HTML? -
I still can not wrap my dome around the built in the dmdual class.
Why should I want to use it just like the following?
I want to know the benefits.
$ URI = $ _SERVER ['REQUEST_URI']; $ NavArr = Config :: get ('Navigation'); $ Navigation = '& lt; Ul id = "nav" & gt; . "\ N"; Forex Currency ($ navArr $ name = & gt; $ path) {$ navigation. = '& Lt; Li '((In_array ($ URI, $ path))' square = 'active' ': incorrect). '& Gt; & Lt; A href = "'. $ Path [1].' & Gt; For the $ name '
using DOMDocument here An example is:
$ Doc = new DOMDocument; $ list = $ doc- & gt; Append Chilled ($ doc-> Create Element ('ul')); $ list- & Gt; Set Attribute ('id', 'nav'); foreign currency ($ navArr $ name = & gt; $ path) {$ listItem = $ list-> Append stanz ($ doc- & gt; create element ('Li'); if (in_array ($ URI, $ path)) {$ listItem- & gt; setAttribute ('class', 'active');} $ link = $ listItem- & gt; append child ($ Doc-> Create Element ('A')); $ Link-> Set Attribute ('Href', $ path [1]); $ link & gt; appendChild ($ Doctor & gt; createTextNode (name $));} Return $ doc-> Save HTML (); This is more verbose, but not too much, and what is probably happening in each step, is obvious.
There is a benefit avoidable character: createTextNode And setAttribute make sure that special HTML characters (quotes, empress and angle brackets) are running properly. Finally, however, for a larger application, you probably want to use the actual templateing language to generate HTML, because the templates are more readable and expandable.
Comments
Post a Comment