简体   繁体   English

如何将自定义变量放入 phtml 文件?

[英]How to get a custom variable into a phtml file?

I have created a custom variable from Magento 2 admin (System > Custom Variables).我从 Magento 2 管理员创建了一个自定义变量(系统 > 自定义变量)。 My custom variable code is "test_var".我的自定义变量代码是“test_var”。

How can I get that value in a phtml file?如何在 phtml 文件中获取该值?

for this you have to use object Manager and load model using its variable Code为此,您必须使用对象管理器并使用其变量代码加载模型

After that you can get its plain value, html value and its name too.之后,您也可以获取其纯值、html 值及其名称。

 <?php 
$model = $this->_objectManager->get('Magento\Variable\Model\Variable')->loadByCode('test_var');
$plain_value = $model->getPlainValue();
$html_value = $model->getHtmlValue();
$name = $model->getName();
?>

The "clean" way is to do this with dependency injection. “干净”的方法是通过依赖注入来做到这一点。

Create your own block:创建自己的块:

namespace MyCompany\MyBlockName\Block;

class MyBlock extends \Magento\Framework\View\Element\Template {

    protected $_varFactory;

    public function __construct(
        \Magento\Variable\Model\VariableFactory $varFactory,
        \Magento\Framework\View\Element\Template\Context $context)
    {
        $this->_varFactory = $varFactory;
        parent::__construct($context);
    }

    public function getVariableValue() {
        $var = $this->_varFactory->create();
        $var->loadByCode('test_var');
        return $var->getValue('text');
    }

}

And use it in your .phtml file:并在您的.phtml文件中使用它:

<?php echo $this->getVariableValue() ?>

Please use this code:请使用此代码:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $variable = $objectManager->create('Magento\Variable\Model\Variable');

    $value = $variable->loadByCode('variableCode')->getPlainValue();
    echo $value;

To get custom variables taking into account the different store views it's possible to use the Object Manager:要获取考虑到不同商店视图的自定义变量可以使用对象管理器:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeID = $storeManager->getStore()->getStoreId();

// HTML VALUE
$objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('your_custom_variable')->getHtmlValue();

// PLAIN VALUE
$objectManager->get('Magento\Variable\Model\Variable')->setStoreId($storeID)->loadByCode('your_custom_variable')->getPlainValue();
// To get the TEXT value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('text');

// To get the HTML value of the custom variable:
Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('custom_variable_code')->getValue('html');

This works in magento 2.2 in a phtml file :这适用于 phtml 文件中的 magento 2.2:

$manager = \Magento\Framework\App\ObjectManager::getInstance();
$value = $manager
         ->get('Magento\Framework\App\DeploymentConfig')
         ->get('shop/url') // other ex: 'db/connection/default/host'
; 

我们 Stenik 团队根据我们的需要开发了 magento 2 模块,可帮助您从任何 phtml 模板访问自定义变量: 下载

添加到 Arnaud 的答案中,使用$var->validate() === TRUE检查代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM