Problem
As the title says, I refuse to use a common template engine. I have my reasons, so instead of doing this:
<ul>
<li><a href = "#">test</a>
<li>{CommonParam}</li>
</ul>
I do this:
function listBlock($array){
echo '<ul>';
foreach($array as $foo){
echo '<li' . $foo['args'] . '>' . '<a href = "'' . $foo['link] . '"'>' . $foo['text'] . </li>';
}
echo '</ul>'
}
// now we'd call listBlock($myarray)
My question is quite simple… as I’m actually developing my own custom framework, I know it’s a bit difficult to maintain this code, but it works great. Is this a good practice?
Solution
In MVC Frameworks, you will often find this question answered a multitude of ways. Here are two principles that I attempt to live by, with regards to this.
-
Separate business logic from presentation logic. If the function in question takes input from another source, and simply does some formatting on it, then it is similar to the view helpers used in most MVC frameworks, and thus is separated from any business logic that processed the input to the function. However, if the function itself is doing logic based upon its input to determine other things about the data, other then simply how to display it, you wind up with a situation where if you have more then one case down the road, this function will begin to grow substantially, and eventually may become impossible to understand, or inefficient.
-
Data processed by the system should be usable in many forms. If your data might be needed for other uses, beyond display, then you may later on find yourself having to refactor, or even duplicate the code, when you realize that you need ‘Ted Developer’ in ‘Developer, Ted’, ‘Ted Developer’ and ‘Ted’ forms, and your function only returns one of the three.