I have a chunk of HTML code (with some PHP vars echoed inside of it) that I need show on multiple times (inside a loop) AND on multiple pages.
So how to keep it DRY?
My ideas admittedly were not brilliant:
-
Putting it in a separate file and including – but then I’ll do an include inside a loop and it showed like bad practice.
-
Putting it in a function – but then I have to make variables global plus it just doesn’t look to me like functions are meant for such usage.
Is there a better way? Is this where maybe OOP has a solution? (I run php 5.3)
Use templates.
Just write your loop inside of the template, as well as all other presentation code.
so, no need to include anything at all.
For the multiple pages you may wish to make a helper function, called from the template.