简体   繁体   中英

Prestashop add css to a module

I am creating a module in prestashop 1.4, say blocktest

modules/blocktest/blocktest.php:

...

public function hookLeftColumn($params)
{
    global $smarty;
    $smarty->assign(array(
        'test' => 'test'
    ));
    return $this->display(__FILE__, 'blocktest.tpl');
}

public function hookHeader()
{
    Tools::addCSS($this->_path.'blocktest.css', 'all');
}

modules/blocktest/blocktest.css:

* { background-color: red; }


Problem:

My css is not included.


What i tried:

In admin > preferences > performances > smarty , i have set cache to no , and force compile to yes . In admin > preferences > performances > smarty , cache is set to no .

Existing modules uses the same css inclusion: Tools::addCSS($this->_path.'blocktest.css', 'all'); , but the css file is in <themeName>/css/modules/<moduleName>/<moduleName>.css . Which is wierd, because $this->_path points to the module folder: modules/<moduleName>/ .

But anyway, I tried to put my css file in <themeName>/css/modules/blocktest/blocktest.css , that doesn't work. Maybe i am missing something

Did you remember about registering a hook for the header during module's installation?

function install() {
    if (!parent::install())
        return false;
    if (!$this->registerHook('header'))
        return false;
    return true;
}

Without it you will have to use "transplant module" function from Admin > Modules > Positions, to do this. Always check with tools like Firebug to verify whether your files are there.

Also, I think there is something missing, can you supply us with the full code of your module? Please, provide us with a Prestashop version that you use as well.

Other solution:

$this->context->controller->addCSS(($this->_path).'style.css', 'all'); 
$this->context->controller->addJs(($this->_path).'script.js', 'all'); 

Greetings,

source info:

http://www.prestashop.com/forums/topic/235476-solucionadoerror-en-mi-1ra-web-warning-function-addcss-is-deprecated-in/

Which is wierd, because $this->_path points to the module folder: modules//

yes it is (weird), but... in the addCSS function, it is overriden with (module) themes css folder

public static function addCSS($css_uri, $css_media_type = 'all')
{
  global $css_files;
   ...
  $css_uri = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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