Magento:何时将变量传递给块,何时不传递?

发布于 2024-09-27 00:26:46 字数 669 浏览 1 评论 0原文

我最近发现了使用分配方法在 _toHtml() 方法中将变量设置到块的强大功能。我的问题是,什么时候适合这样做,什么时候不适合?我正在创建一个新模块,在我看来,将所有变量分配给块并在视图文件中引用这些变量而不是设置类似的东西

<?php
  $var1 = $this->getVar1();
  $var2 = $this->getVar2();
?>

<div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>

是很好的,只是在块类并只调用 phtml 文件中的变量?那么你的模板文件看起来就像这样,

<div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>

这会删除模板中的更多 php 代码,我认为这很好?

我唯一能想到的是,当其他开发人员处理模板文件时,很难知道设置了哪些变量。如果我在顶部添加注释,所有变量大多在 x 块类中设置,这将有所帮助,或者如果他们正在调试,他们将看到所有设置的变量,但我想它仍然可能令人困惑,我假设这就是 Magento 的原因只是做得很谨慎。

但我正在寻找其他人对此的意见以获得最佳实践。

I have recently found the power of setting variables to a block in the _toHtml() method, using the assign method. My question is, when is it good to do this and when is it not? I am creating a new module and it seems to me it is much nice to just assign all the variables to the block and just reference those variables in the view file rather then setting up something like this

<?php
  $var1 = $this->getVar1();
  $var2 = $this->getVar2();
?>

<div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>

Isn't better to just set those in the block class and just call the variables in the phtml file? Then your template file would just look like

<div id="<?php echo $var1 ?>"><?php echo $var2 ?></div>

This would remove more php code being in the template, which I think is good?

The only thing I can think of is, it will be harder to know what variables are set when other developers are working on the template file. If I put a comment at the top that all variables are mostly set in x block class that would help or if they are debugging they will see all the variables set but I guess it could still be confusing, and I am assuming this is why Magento has only did it sparingly.

But I am looking for anyone else's opinion on this for best practices.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2024-10-04 00:26:46

Magento 中的“正确”方式实际上可能更接近于此:

<div id="<?php echo $this->getVar1(); ?>"><?php echo $this->getVar2(); ?></div>

拥有更少的代码可能是一件好事,但前提是您了解权衡。为了节省两行(或者如果使用上述技术则不保存行),您实际上失去了一些灵活性。

Magento 如此依赖神奇的 get*set* 方法的原因是它们可以在类中以透明的方式重写。

为了便于讨论,假设有人重写了您的类并决定 var1 应该动态计算,而不是静态设置(这种情况可能不会经常发生在您身上,但 Varien 需要对于核心课程,请考虑到这一点)。如果您手动设置变量并按原样使用它们,这可能需要您更改代码中的多个引用。但是,通过使用神奇的 get* 方法,属性的计算要简单得多:

public function getVar1() {
    $value = $this->_getData('var1');
    // perform calculations here
    return $value;
}

此公式甚至不会阻止与调用等效的 set* ,也不需要任何依赖此方法更新代码。因此,请尽可能使用魔术方法,因为它们对于框架来说更惯用,并且将允许您和其他人在添加代码时获得最大可能的灵活性。

希望有帮助!

谢谢,

The "right" way in Magento might actually be closer to this:

<div id="<?php echo $this->getVar1(); ?>"><?php echo $this->getVar2(); ?></div>

Having less code can be a good thing, but only when you understand the tradeoffs. For the sake of saving two lines (or no lines if you use the above technique), you are actually losing some flexibility.

The reason that Magento relies so heavily on magic get* and set* methods is that they can be overridden in a class in a transparent way.

Let's say, for argument's sake, that down the road someone overrides your class and decides that var1 should be calculated on the fly, rather than set statically (this may not happen to you often, but Varien needed to take this into account for core classes). If you set variables manually and use them as such, this would likely require you to change several references in code. However, by using magic get* methods, calculations for attributes are much simpler:

public function getVar1() {
    $value = $this->_getData('var1');
    // perform calculations here
    return $value;
}

This formulation doesn't even block the set* equivalent of the call, nor does it require any updates to code relying on this method. So, use the magic methods when you can, as they are more idiomatic to the framework and will allow you and others the maximal possible flexibility when working with your code additions.

Hope that helps!

Thanks,
Joe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文