【发布时间】:2011-06-02 22:20:29
【问题描述】:
我知道“自定义变量”以及它们如何在电子邮件模板和静态块中与 {{ }} 括号一起使用。
但是,我想在模板代码中使用它们,即 view.phtml。
我希望能够访问“可变纯值”以检索转换值,即数字/字符串作为给定“可变代码”的数字。
【问题讨论】:
标签: magento
我知道“自定义变量”以及它们如何在电子邮件模板和静态块中与 {{ }} 括号一起使用。
但是,我想在模板代码中使用它们,即 view.phtml。
我希望能够访问“可变纯值”以检索转换值,即数字/字符串作为给定“可变代码”的数字。
【问题讨论】:
标签: magento
一段时间以来,我一直在这样做以创建可通过管理界面编辑的各种消息,这样当当下的风格发生变化时,我就不必去挖掘代码了。
要使用代码 custom_variable_code 访问自定义变量的 plain 值,请使用:
Mage::getModel('core/variable')->loadByCode('custom_variable_code')->getValue('plain');
注意:单一商店不显示变量范围的商店选择下拉菜单。这个答案在技术上是不正确的,为了在拥有多家商店的情况下让自己在未来得到证明 --> 请参阅下面的@Mark van der Sanden 答案并给他一个支持。
【讨论】:
很遗憾,所有其他答案都不是 100% 正确的。像这样使用它(注意setStoreId() 以获得正确商店视图的值):
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('text');
或者获取html值:
$value = Mage::getModel('core/variable')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCode('variable_code')
->getValue('html');
如果没有定义 html 值,getValue() 在您请求 html 值时返回文本值。
【讨论】:
setStoreId(),但最好还是这样做以防万一。
Stackoverflow 几乎又来了。以为是这样:
Setting a global variable in Magento, the GUI way?
但不是,这是:
$angle = Mage::getModel('core/variable')->loadByCode('angle')->getData('store_plain_value');
【讨论】:
我认为您可以通过在模板块中使用方法来实现这一点的唯一方法,该方法将输出所需的结果。
例如在模板 view.phtml 中你有以下代码:
<div id="title_container">
<h2><?= $this->getTitle(); ?></h2>
</div>
该函数可以表示您的可变代码,任何与标题中显示的内容有关的逻辑都应放在块中。
为了澄清起见,块是变量 $this
如果您不确定块的实际类名是什么,您可以执行以下操作:
Mage::log(get_class($this));
在 var/log/system.log 中,您将打印该模板的块的类。
这是最好的方法。
HTH :)
【讨论】:
// 获取自定义变量的TEXT值:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');
// 获取自定义变量的 HTML 值:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');
// 店铺id设置为自定义变量,可编辑多个店铺
【讨论】:
注意:自定义变量可能对不同的商店有不同的值。
所以要使用代码 custom_variable_code
使用这个:
$storeId = Mage::app()->getStore()->getId();
$custom_variable_text = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('text');
$custom_variable_plain_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('plain');
$custom_variable_html_value = Mage::getModel('core/variable')->setStoreId($storeId)
->loadByCode('custom_variable_code')
->getValue('html');
【讨论】: