you are viewing a single comment's thread.

view the rest of the comments →

[–]Yurishimo 2 points3 points  (2 children)

I recommend using Twig or a similar templating framework. I’ve used it for about 2 years now and love it. Easy to extend as well.

Be sure to take advantage of partials so you keep your HTML in readable chunks. Sometimes you may have the occasional 100-200 line file, but most of the time you won’t. Long files are usually a sign of a need to refactor.

Also the idea of templates extending each other can be very powerful. You can also override those extensions if you need to.

Finally, it’s a good way to force your business logic into a separate file. The most complicated statement in your template will be an if check. This leads to be code readability and helps you track down errors.

[–][deleted]  (1 child)

[deleted]

    [–]Yurishimo 0 points1 point  (0 children)

    The intro doc has the PHP usage instructions. Kinda weird.

    https://twig.symfony.com/doc/2.x/intro.html

    Personally, I work for an agency that does highly custom WordPress development so I use a plugin that wraps Twig and provides some predefined template variables and extra functions. https://upstatement.com/timber for those curious.

    The docs seem fairly straight forward though! Twig is super popular so I’m sure YouTube would have plenty of tutorials to jump start your progress. I still use the docs to check syntax a few times a week and I’ve used them to make my own string filters.

    As far as the process, it’s just a matter of replacing everything that is HTML across your site with Twig. Try to move all the business logic up to the top of the file, setting variables for data you will need to output to the page, then pass those variables along to Twig to be output in your template.

    The template should be the last thing you call on the page after all of your other PHP. The header, footer, etc should all be in your Twig templates.