Magento:什么时候将变量传递给块,什么时候不传递? [英] Magento: When to pass variables to a block and when not to?

查看:53
本文介绍了Magento:什么时候将变量传递给块,什么时候不传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了使用Assign方法在_toHtml()方法中将变量设置为块的功能.我的问题是,什么时候做这个好,什么时候不做呢?我正在创建一个新模块,对我来说,将所有变量分配给该块并在视图文件中引用这些变量,而不是像这样设置类似东西

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>

不是只在block类中设置它们,而只是在phtml文件中调用变量?然后您的模板文件看起来就像

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>

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

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

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

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.

推荐答案

Magento中的正确"方法实际上可能更接近于此:

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.

Magento非常依赖魔术get*set*方法的原因是,可以在类中以透明方式覆盖它们.

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.

让我们说说,为了争辩,有人在以后重写您的课程,并决定var1应该是即时计算的,而不是静态设置的(这可能不会经常发生在您身上,但是Varien需要采取这种做法考虑到核心课程).如果您手动设置变量并如此使用它们,则可能需要您更改代码中的多个引用.但是,通过使用魔术get*方法,属性的计算要简单得多:

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;
}

此公式甚至不会阻塞调用的set*等效项,也不需要依赖此方法的任何代码更新.因此,请尽可能使用魔术方法,因为魔术方法对框架而言更惯用,并且在处理代码添加时将为您和其他人员提供最大的灵活性.

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.

希望有帮助!

谢谢, 乔

这篇关于Magento:什么时候将变量传递给块,什么时候不传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆