I'm dealing with code that renders HTML pages. I'm cleaning up an old, messy PHP code base.
Let's say I have a template that loops through a long list of rows in a database. I can load my data into an array and pass that array to the template in order to separate the view from the other logic, or I can pass in the PDOStatement
object, which is a Traversable
(insert any language's iterable interface here), which will receive rows as it loops.
Is the second method approprate?
It seems like it should be appropriate to me, there is no logic in the template, it uses a foreach, just like it would with an array, but the difference is, under the hood, each of the iterations of the loop are pulling data from a database. If an error were to occur, it's too late to handle it well after the headers and part of the page have been sent.