Magento 2 - 如何更改 php 文件中的 less 变量?

Magento 2 - How to change the less variables in the php files?

我正在使用 Magento 2。我想在扩展块中设置颜色和其他 less 变量,例如:@primary__color.

有人知道怎么做吗?

我是通过使用管理颜色配置从 phtml 模板文件生成自定义 css 文件来实现的。

protected function generateStoreCss($type, $storeId) {
        $store = $this->_storeManager->getStore($storeId);
        if(!$store->isActive())
            return;
        $storeCode = $store->getCode();
        $str1 = '_'.$storeCode;
        $str2 = $type.$str1.'.css';
        $str3 = $this->_cssconfigData->getCssConfigDir().$str2;
        $str4 = 'mypath/css/'.$type.'.phtml';
        $this->_coreRegistry->register('cssgen_store', $storeCode);

        try {
            $block = $this->_layoutManager->createBlock('Vendor\Module\Block\Template')->setData('area','frontend')->setTemplate($str4)->toHtml();
            if(!file_exists($this->_cssconfigData->getCssConfigDir())) {
                @mkdir($this->_cssconfigData->getCssConfigDir(), 0777);
            }
            $file = @fopen($str3,"w+");
            @flock($file, LOCK_EX);
            @fwrite($file,$block);
            @flock($file, LOCK_UN);
            @fclose($file);
            if(empty($block)) {
                throw new \Exception( __("Template file is empty or doesn't exist: ".$str4) );
            }
        } catch (\Exception $e) {
            $this->_messageManager->addError(__('Failed generating CSS file: '.$str2.' in '.$this->_cssconfigData->getCssConfigDir()).'<br/>Message: '.$e->getMessage());
        }
        $this->_coreRegistry->unregister('cssgen_store');
    }

然后将该 css 文件导入到布局中。