← All writing
Culture · · 7 min

Logic in templates

PHP Careers

A category page on one of our stores took a few seconds to draw, and for a while I assumed that was the images, because images are my department.

It wasn’t the images. It was six lines I put in that template myself, back in my first weeks here, copied out of a different template because they looked like the ones I needed. They were the ones I needed. They also went to the database once per product, and the page holds 24 of them.

I’d have told you at the start of this year that I don’t write PHP. I said it in a way that sounded like a boundary instead of a gap. Front end, that’s me, I do the part you can see.

I’ve written PHP nearly every working day since I got here. Nobody assigned it to me. There was no conversation about it and no course, and no moment where somebody said “you’re doing this now.” It happened the way a hill happens while you’re walking.

Foreach and find out

Here’s the block, more or less as I left it.

catalog/product/list.phtml
<?php foreach ($_productCollection as $_product): ?>
  <?php $p = Mage::getModel('catalog/product')->load($_product->getId()); ?>
  <li class="product">
    <span class="price"><?php echo $this->helper('core')->currency($p->getFinalPrice()); ?></span>
  </li>
<?php endforeach; ?>
PHP

The loop is fine. The line inside it is the whole problem. I have to say out loud what it does, because at the time I could not have told you.

Mage::getModel('catalog/product')->load() goes and gets a product out of the database. All of it, every attribute, in a handful of queries. I was already handing it a product, sitting in a collection the page had loaded before it ever reached my file. So I asked for something I’d already been given, once per row, and then used the copy instead of the original.

The fix, once one of the back-end developers had read the sentence out to me, was deleting the line and using the product that was right there. Under a minute. Nothing had failed in the whole time it sat there either, since the prices were correct and the page just took a beat longer than it should have, which I think is the kind of wrong that survives a long time. Nobody files a ticket about a page that works. (I had of course checked that the prices were right, that being the only thing I knew how to check!)

Where I thought the line was

At the last place, templates were mostly HTML with some tags dropped into them, and the border between markup and logic was roughly visible. You could stay on your side of it for a whole project without especially trying.

Here the templates are PHP. Not “have PHP in them.” They are PHP. The file that draws a product list is a .phtml, the loop in it is an actual foreach instead of a template directive dressed up as one, and inside the loop is a method call on an object I didn’t instantiate, handing back something I need to know a little about before I can print it.

So on day one I’m changing markup. By the end of that week I want to know why a collection came back empty. A month in I’m reading the class that builds it, which by now I’ve had a fair amount of practice at.

At no point did I cross a line, because there wasn’t one to cross. There was a gradient, and I’d been calling it a wall with all the confidence of somebody who has never once gone and checked. Overriding a template had already taught me that the file I’m editing is a position in a lookup order instead of a thing I own. This was that same lesson one layer down, which I somehow managed to receive as brand new information.

Doing it badly with witnesses

The hard part was never the language. PHP is not hard. The hard part was being visibly mediocre at something in a room where two people do it well and get paid for it.

There’s a particular flavor of dread in asking a question you suspect has a one-word answer. I sat on questions for days at a time. I wrote things that worked and were embarrassing and quietly hoped nobody would open that file. The six lines were me not asking, and I’m not going to pretend that’s behind me.

It actually moved when that same developer reviewed something of mine and left a comment that started “this works, and here’s what I’d do instead.” Not “this is wrong.” The difference sounds small and it was the whole thing, because it established that my code was allowed to exist while being improvable, which is the only condition under which a person can learn in front of an audience.

I wrote a whole post last December about the badly done version being the only one on offer. It turns out I believed that exclusively about side projects, where the audience is nobody. Believing it about work, in a repo other people read, is a much bigger ask, and I was nowhere near it a year ago.

Free with purchase

The odd part is what the language turned out to be a side effect of.

I understand data now, a bit, in a way I didn’t. Not PHP specifically. The fact that behind every list on a page there’s a query, and the query has a cost, and the reason the category page crawled was not the CSS. I’d have nodded along to that sentence in January. I couldn’t have found you an example of it.

I’ve also lost the ability to say “that’s a back end problem” as though it were a routing instruction. Sometimes it honestly is. But I’d been saying it about anything that happened before the HTML existed, which covers most of what decides whether the page is any good.

And it made me better at the job I actually have, in a small practical way. I can tell now whether the awkward thing I’m being asked to do in a template is awkward because I’m doing it wrong, or awkward because the data is arriving in a shape nobody picked on purpose. Those need two completely different conversations and I used to have the first one every time.

None of it was a plan, which is the part I keep turning over. The thing I’d say to myself a year ago isn’t “learn PHP,” it’s that the label on your job describes what you did last year instead of fencing off what you’re allowed to touch. I was using mine as both, and the second use was quietly costing me things I’d never have counted.

The other half is that the fastest learning I’ve done this year had nothing to do with courses, which I’ve taken plenty of and gotten real value out of. It came from needing one specific thing, on an ordinary day, for a real reason, with somebody waiting on me. Motivation was never my problem and structure was never my problem. Proximity was the problem, and a job fixes proximity in a way I haven’t found anything else to match. I still wouldn’t call myself a PHP developer, though I notice that’s a modesty thing now instead of a factual one, which is its own small tell.

Read similar posts
10 min

Something that can go red

Somebody asked me for something to read about naming conventions, so I sent them a post I wrote in 2015 and then made the mistake of reading it myself.

9 min

Nobody demos an upgrade

The best possible outcome of a migration is that nothing happens, which looks exactly the same from the outside as having done nothing at all.