Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ever a good reason to use PHP inside ExpressionEngine templates?

I've heard mixed things about this. I know it's easy when you're stuck to simply enable PHP in the template and hack your way around a problem. However, it almost always introduces potential security issues, and makes a mess of your template to boot.

In most cases where PHP is used, it would be cleaner to write a simple plugin in PHP to achieve what you need.

My question is this: Is it ever acceptable to use PHP inside ExpressionEngine templates? Or is this considered bad practice? If you were a developer who inherited a site I coded, would you cringe if you saw PHP inside templates?

like image 760
Adrian Macneil Avatar asked Oct 27 '12 01:10

Adrian Macneil


2 Answers

This could just as easily be a question on the merits of eval() and whether it should ever be used. Most of the answers to questions along those lines are equally applicable here.

My view is that enabling PHP in templates is always a Bad Idea. At best, it's not a terrible idea, but there's always a better option. A custom addon will invariably be a better approach, even with seemingly harmless code snippets. I think it's also most likely to be used by those least qualified to use it. Personally, when I started building sites with EE my PHP skills ranged somewhere between 'totally inept' and 'knows enough to be dangerous'. At that time, if I came across a limitation in EE's code I'd often implement a solution via some inline PHP, partly because the idea of developing a custom addon seemed too daunting and partly because clicking a little 'enable PHP' radio button was so simple. Years later, whenever I need to update one of these sites, I definitely do cringe. Here are some of the side effects of PHP in templates:

  • Potentially introducing serious security vulnerabilities.
  • Increasing load times, potentially by a lot.
  • Debugging code becomes harder (good luck figuring out which file/line number has that error).

With tools like Pkg.io you can have the groundwork for an addon in place in 30 seconds. It requires a tiny amount of extra effort, but the gains are well worth it.

like image 87
Dom Stubbs Avatar answered Oct 03 '22 23:10

Dom Stubbs


Let's imagine I'm answering this question as part of the 80% of people building websites with ExpressionEngine who may consider themselves as designers and or people who have little to no PHP experience:

Is it ever acceptable to use PHP inside ExpressionEngine templates?

No it's not acceptable, please don't do it!

Or is this considered bad practice?

Yup, pretty much.

If you were a developer who inherited a site I coded, would you cringe if you saw PHP inside templates?

Definitely would cringe and maybe even curse a little.


Okay, now let's imagine I'm answering as part of the 20% who would consider themselves as an EE developer and or who have most experience programming over say designer all day:

Is it ever acceptable to use PHP inside ExpressionEngine templates?

Once in a while it's acceptable. For example you might find a need to turn on PHP for this Stash setup. It really depends on the complexity of what you are trying to achieve. Most things should go into an add-on whenever possible and being "lazy" doesn't count as a time when PHP is acceptable in templates.

Or is this considered bad practice?

ExpressionEngine and Codeigniter for that matter provide a great base to build add-ons and best practices include proper separation when ever possible. This includes separation from PHP and Templates; more often than not.

If it can go into an add-on it should as mentioned before. With that said, there isn't really anything limiting you from using PHP in templates except knowing that this may have security implications, cause future headaches for clients and other devs/designers etc.

If you were a developer who inherited a site I coded, would you cringe if you saw PHP inside templates?

Definitely wouldn't cringed (unless your PHP was a disaster right from the start) though, would probably question why such simple and even complex code wasn't where it belonged; in an add-on.

Note: I pulled the percents out of thin air though, they quite possibly could be close to reality.

Also note: I realize the use of designer/developer in my answer could be seen as stereotyping though, wasn't quite sure how to avoid it. Hope no one takes offense.

like image 45
Natetronn Avatar answered Oct 03 '22 23:10

Natetronn