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.