Web lists-archives.com

Re: [PHP] MVC: code in views




On Sun, Jan 3, 2016 at 4:43 PM, Ashley Sheridan <ash@xxxxxxxxxxxxxxxxxxxx>
wrote:

>
> Just wanted to get some thoughts from others on the list. Recently I've
> been trying to avoid using any PHP code at all in my views at all, not
> even an if statement or for loop. The general idea is that I want to
> make it as easy as possible for developers who do no PHP at all (I work
> with a lot of them) to be able to work more easily.
>
> So far I've been doing this by moving the little logic bits that might
> otherwise end up in the views to controllers or the template engine to
> handle, and use a basic templating system that the frontend developers
> can use.
>
> What are peoples' thoughts on this? Am I going down the wrong path with
> wanting to keep my views HTML-only, or is this something that any of you
> are trying to do? I'm finding the benefits only when working with others
> in a team, otherwise I've no problem with PHP in views myself.
>
> As an aside, it has made things slightly easier when it comes to using
> CodeSniffer to analyse code quality, because a lot of the tests were
> throwing false positives because of the mix of HTML and PHP in the view
> files. Keeping things separate now means that an error in a view is a
> true error.


Hi Ashley,

I've tried several approaches over an extended period of time. Sometimes
PHP was everywhere. Sometimes templating regions were declared without PHP
(header, footer, etc.), but I used PHP for loops and logic. And, sometimes
I removed all PHP from templates. I made myself stick with an approach for
several projects to make sure I've had enough time and experience to note
pros and cons.

There was no clear winner for me, but there were certainly clear use cases.
As you noted, while working with front-end designers who don't know PHP,
I've found advantages to keeping PHP out of the templates completely. That
said, when I'm working on a project by myself, I can much more clearly
express something using plain old PHP, and I can often more easily refactor
for performance, etc.

That all said, my typical choice now is to use a custom pre-processor that
I've built so most files don't need PHP, includes for headers and footers
can happen once instead of on each request, and it's easy to integrate PHP
anytime I need that power.

Enjoy the exploration!

Adam