使用 Prestashop 模块动态嵌入 inline-CSS 到 head

Dynamically embed inline-CSS to head using Prestashop module

如何动态嵌入通过后台输入的内联 CSS。在 Prestashop 中注册资产只有两种注册方法。

public function hookDisplayHeader()
{
    $this->context->controller->registerJavascript('id', 'path/to/file.js');
    $this->context->controller->registerStylesheet('id', 'path/to/file.css');
}

registerStylesheet 方法有一个名为 inline 的选项,但它要求内容首先位于现有文件中。虽然我在配置中存储了 CSS 代码。

并且尝试 echo hookDisplayHeader() 中的内容会导致它被打印在 <html> 开放标签上方。

在模块的 hooks 目录中创建一个 TPL 文件。

然后从hookDisplayHeader()函数中的配置中获取CSS代码值并传递给smarty:

$custom_css = `GET CUSTOM CSS FROM CONFIG`;
$this->smarty->assign('yourcustomcss', $custom_css);    
return $this->display(__FILE__, 'dummy_file.tpl');

然后在TPL文件中:dummy_file.tpl

{if isset($yourcustomcss)}
    <style>{$yourcustomcss nofilter}</style>
{/if}

TPL 文件应该位于以下目录中:

/yourmodule/views/templates/hook/dummy_file.tpl