1
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ Depends where the array is coming from i guess, is it coming from a database, is it user inputted or is simply sent as arguments from a page? \$\endgroup\$
    – CodeX
    Jun 27, 2014 at 18:29
  • \$\begingroup\$ It doesn't comes from a database, however, it's actually possible. And also, I use it somewhere, coming from a database. But never user inputted. \$\endgroup\$ Jun 27, 2014 at 18:49
  • 1
    \$\begingroup\$ "You'll find me in ship but not in bottle" Can you tell me where the array is coming from please \$\endgroup\$
    – CodeX
    Jun 27, 2014 at 19:20
  • 1
    \$\begingroup\$ This looks like a down grade from the template engine you're referencing. You increased the complexity and have mixed some PHP in, reducing readability. It looks like it'd be a pain in the ass to have to maintain. However, if you have such special requirements, do what needs to be done. \$\endgroup\$
    – Alex L
    Jun 27, 2014 at 21:25
  • \$\begingroup\$ There are popular web frameworks who do not use a built in templating engine. For example Yii. \$\endgroup\$
    – RustyFluff
    Jun 28, 2014 at 2:21

1 Answer 1

1
\$\begingroup\$

Answer courtesy of JC

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.

  1. 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.

  2. 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.

\$\endgroup\$
1
  • \$\begingroup\$ You should come join other Code Reviewers in the Second Monitor! It looks like you have an interest for the codez \$\endgroup\$
    – Alex L
    Jun 27, 2014 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.