Categories


Archives


Recent Posts


Categories


Template Variable Naming Conventions

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Posting this to archive a twitter conversation between Vinai Kopp and myself.

I’ve long wondered what the naming conventions were for variables in Magento’s phtml template files. Many templates use the $_underscore convention for all their variables. Other templates don’t. People have speculated that Magento is warning users not to touch these variables, since that’s been the typical semantic meaning of the underscore through programming history.

I asked twitter what was up with the underscores, and Vinai responded with two informative tweets.

To the first question he said

Yes. Convention says variables that are declared within the template should start with $_* (vs ->assign(‘var’, $val))

and when I asked about discrepancies

Oversights or not enough training for template devs 🙂 Yes, I’m afraid there are a number of those.

Eureka, concrete advice from Magento’s leading European developer trainer. Template variables are named with underscores to indicate to developers that the file you’re looking at is a template. If you’re looking to stick to Magento’s official conventions, start updating those templates!

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 17th July 2012