I’m trying to figure out the most efficient way to implement RoR-style partials/collections for a PHP template class that I’m writing. For those who aren’t familiar with rails, I want to iterate over a template fragment (say a table row or list item) located in a separate file. I want to do this without resorting to eval or placing an include within the loop.
I’ve seen a similar post that addresses single partials, which are trivial, but nothing that covers implementing partials in a collection. I’ve been thinking about this so long my head hurts and I’m afraid I’m overlooking an obvious solution. I’m hoping someone here can suggest an elegant solution that, again, doesn’t require eval or include within the loop. TIA.
You need a templating engine with that can process includes on its own and then eval the whole thing at once. Much like c preprocessor works.
Step 1 (source template):
Step 2 (after preprocessing):
Step 3 (final rendering)
In this way you can avoid the overhead of evaling/including subtemplate on every loop step.