views:

45

answers:

3

I've come across dozens of scripts in which html is echo'd out instead of being stored. I'm wondering if it's generally a good practice of always storing the html in a string due to the flexible nature?

A random example would be that I have a function that returns the html for a dynamic subnavigation. I'm printing the opening div tag, printing the contents of it, and then printing the end div tag separately:

<div id="nav">
<?php echo $nav->getHTML();?>
</div>

However, I now have to assign a special class to the #nav div based on whether the amount of list items and sum of characters inside of $nav->getHTML() exceeds a certain amount, in order to separately assign a different line-height and height. For this I have to load it into a DOMDocument and use DOMXpath and make some evaluations.

      html = '<div id="nav">';
      html.= $nav->getHTML();

      dom = new DOMDocument(); // create a DOM tree 
      xpath = new DOMXpath($d); // create a DOM Xpath tree

      // bunch of DOM querying/manipulation

      html.= '</div>';

I'm thinking, shouldn't it be best to always store something like this to make things more flexible for future requests that require string manipulation? Or am I just needlessly overworrying? Or perhaps I'm going about this the wrong way, and need to refactor my class to do the DOM querying inside instead of outside?

EDIT: After I determine whether the ul will span multiple rows ( this is a horizontal list ) based on a limit of characters ( let's say 200 ) I'll add a special class to the #nav item and from then on do the styling in CSS.

Disclaimer: I don't want to rely on JS at all. I'm aware I can solve it, but I want it to immediately render properly.

+1  A: 

I'd argue that since, in this instance, you know that string is subject to manipulation, then storing it now makes sense. If you're building a template or other view that's not subject to any server-side manipulation, then you'll find yourself going crazy keeping track of all of your assignments, most of which you'll never touch.

Now, for your actual problem, it sounds like what you don't need is server-side manipulation, but some hardcore CSS. That would probably be in keeping with the general best practice of separation of style, content, and behavior.

dmrnj
Solving this purely with CSS isn't feasible. I'm dynamically manipulating an absolutely positioned `ul` that needs to stick to the bottom of another element. If there are multiple rows of `li`s then I need to alter the height and line-height of the `ul`. This also needs to be cross-browser compatible. I can't dynamically "know" if the `ul` is spanning multiple rows or not with purely CSS.
meder
Why not? Abs. positioned at the bottom requires a bottom:0; it doesn't require a height. If you set some clever styling (display:inline-block; overflow:auto; to the UL, and float:left; to the LI's) then the li's should wrap to a new line as if they were words in a paragraph, and your ul would maintain a fluid height.You can also use jQuery to assist in visual calculations, appending each li to the UL so long as their total calculated width is less than the ul's width, and multiplying the height of the ul accordingly.
dmrnj
+1  A: 

Personally, I'm reluctant put presentation details (like computing line-height or height properties you mentioned) in server-side script. I would try to handle it with CSS or (in worst case) javascript.

In your case, I don't know much $nav, but can't you calculate line-height and others during its generation, not from the source? Say, have getHeight and getLineHeight methods in addition to getHTML.

Nikita Rybak
+1  A: 

Parsing generated HTML to derive business/rendering logic is not a good idea. I would rather have another function called "getClass" that returns the class based on the amount of list items or sum of characters inside $nav->getHTML().

<div id="nav" class="<?php echo $nav->getDisplayClass();?>">

<?php echo $nav->getHTML();?>

</div>

darkWing
The only caveat is I'm not really allowed to mess with the class.
meder