0

What's the best way to create a temporary variable inside a .phtml template while abiding by Magento's architecture?

Example

File: /template/catalog/product/view.phtml

<?php
$myVar = $_product->getAttributeText('color');

if ( empty($myVar) ) {
    // does not exist
} else {
    // show the attribute
}
?>

Beyond this expression $myVar isn't needed anywhere else.

Note: I'm not looking for alternative ways to write this code that avoid creating vars. For the sake of argument, assume a scenario where creating a temporary var is necessary.

What should $myVar be?

  1. $myVar
  2. $namespaced_myVar
  3. $_myVar
  4. Magento's registry pattern http://alanstorm.com/magento_registry_singleton_tutorial
  5. Something else...

Looking for a "real world" solution more than a purist answer. How would you write this?

Answer

Combined between Ben's answer and this bit from Alan/Vinai's conversation https://twitter.com/VinaiKopp/status/225318270591442945 — this is how I'm going to write it:

If the anything more than basic logic is needed, I'll extend the class with new methods.

Otherwise, I'll create new vars in the local scope like so:

$mynamespace_myVar = 'xyz';

This is what I like about it:

  • The $mynamespace_ reminds me I created this and not Magento
  • It also makes it highly unlikely another developer overwrites my vars

This is what I don't like:

  • It's un-pure and potentially corruptible, but I probably only need this <5 times for an entire site so it's reasonably shielded.
  • Not using $_ to show the var is local to this template is not "the Magento way" but it makes the code more readable.

So my templates will mostly have code like this:

$gravdept_someNiceData = true;
Brendan Falkowski
  • 733
  • 1
  • 5
  • 17

1 Answers1

3

Some history: https://stackoverflow.com/a/3955757/833795

Re 1, 2, & 3: Differentiating between these choices involves getting into "purist answer" territory, as they are all local variables.

Using the registry pattern is uncalled for, as the desired scope is stated to be local to the rendering of the template.

Based on your example, an appropriate construction in Magento might be:

<?php if ($_product->getColor()): ?>
    <h2> I HAZ COLOR </h2>
<?php else: ?>
    <h2> I NO HAZ COLOR </h2>
<?php endif ?>

If there is anything but the simplest test of return values it's appropriate to add this logic as a method of the block class (using a rewrite), as it is in fact view logic which you've mentioned should be local to this context only.

Community
  • 1
  • 1
benmarks
  • 23,384
  • 1
  • 62
  • 84
  • See also: http://magento-quickies.tumblr.com/post/27440772714/template-variable-naming-conventions – Alana Storm Sep 19 '12 at 17:51
  • I guess I'm asking for the best option within the local scope. The registry pattern was only suggested because it has a mechanism to prevent overwriting vars. – Brendan Falkowski Sep 19 '12 at 18:22
  • While the rewritten code works, it was only meant as an example of using a var in a template. In my *actual* use case minor manipulation/comparison is needed, and creating a var or two improves readability. Extending the class with a new method is overkill for something that could (more easily) be maintained in the template (in this case). – Brendan Falkowski Sep 19 '12 at 18:29
  • Sounds like you have your answer: create some convenience variables & profit :-) – benmarks Sep 19 '12 at 18:31