SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Do you use the DOM Extension?

    I find doing this sort of thing quite ugly and somewhat "primitive":
    PHP Code:
    echo "\t\t<p";
    if (
    $something) echo ' class="current"';
    echo 
    ">$text</p>\n"
    or:
    PHP Code:
    if ($something$c ' class="current"';
    echo 
    "\t\t<p$c>$text</p>\n"
    Doing this sort of thing with javascript is much nicer:
    Code Javascript:
    var p = document.createElement('p');
    if (something) p.className = 'insect';
    parent.appendChild(p);
    It might be more verbose, but it's more readable and it's logical. Even if you're using templates, there must come a point where you have to go through the ugliness above (or something similar to it), so I wonder how MVC advocates deal with this. Do you use PHP's DOM extension? If not, then what do you do?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I only use the DOM extension for two or three reasons:

    1. if I really, really, really need to ensure the well-formedness of my output on the fly
    2. if I'm developing a really complicated view that requires me to sort dynamic objects into a pre-built HTML container (like, for instance, looping through an unordered array of calendar events and putting them all in the appropriate date cells in a calendar table)
    3. if I need to dynamically replace certain elements or attributes in an HTML string with others (for example, if I'm trying to stick to valid XHTML, I sometimes use DOM to replace all 'target="_blank"' attributes with 'rel="external"')


    To tell you the truth, though, only #2 has really turned out to be helpful for me. #1 is terrible overkill given that I can validate my pages manually most of the time. #3, meanwhile, is problematic due to how strictly the DOM extension outputs XHTML...I haven't yet figured out a workable way to serve it successfully to all browsers.

    If you want to improve the readability of your original example, probably the best thing to do ("best" in the sense of keeping your scripts simple and fast) would be to break it up into smaller chunks and use indentation to show what belongs to what, i.e.:

    PHP Code:
    echo "\t\t";
    echo 
    '<p';
        if (
    $something) {
            echo 
    ' class="current"';
        }
    echo 
    '>';
        echo 
    $text;
    echo 
    '</p>';
    echo 
    PHP_EOL
    That's more readable to me...but there are a lot more ways you could do it, and I won't say that any of them are right or wrong; readability is, in many cases, a matter of preference.

    Personally, though, I find this a lot more readable, and it doesn't result in the same kind of performance hit that the DOM extension would give you. It's still somewhat "primitive," as you say, but primitive can be a very good thing when it keeps your application fast. I haven't benchmarked this in awhile so I could be wrong...but I can't imagine that string concatenation would be slower than loading an object-oriented XML interpreter

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's very true, primitive is also fast. I hadn't thought of the performance hit of invoking the extension's engine, I guess it's only useful, like you say, for larger projects. Thanks for your reply.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •