Prestashop基本模块,小调整


Prestashop basic module, small adjusting

我为 Prestashop 1.4 创建了文件 mymodule.php

<?php
if (!defined('_CAN_LOAD_FILES_'))
    exit;
class MyModule extends Module
{
    function __construct()
    {
        $this->name = "mymodule";
        $this->version = "1.0";
        $this->author = "Tomtop";
        $this->tab = "front_office_features";
        $this->_postErrors = array();
        parent::__construct();
        $this->displayName = $this->l("My Module Name");
        $this->description = $this->l("This is my module description.");
    }
    protected function setConfig($key,$value)
    {
        return Configuration::updateValue($this->name.$key,$value,true);
    }
    protected function getConfig($value)
    {
        return Configuration::get($this->name.$value);
    }
    protected function deleteConfig($value)
    {
        return Configuration::deleteByName($this->name.$value);
    }
    function install()
    {
        if (!parent::install()
            OR !$this->registerHook('home')
            OR !$this->registerHook('footer')
        )
            return false;
        return true;
    }
    public function uninstall()
    {
        parent::uninstall();
        return true;
    }

    public function hookHome($params)
    {       
    }
    public function hookfooter($params)
    {       
    }

    private function _postProcess()
    {
        $this->_html .= '<div class="conf confirm">'.$this->l("Updated")."</div>";
    }
    public function getContent()
    {
        $this->_html .= "<h2>".$this->displayName."</h2>";
        if (Tools::isSubmit("submit"))
        {   
            $this->_postProcess();
        }
        $this->_displayForm();
        return $this->_html;
    }   
    private function _displayForm()
    {   
        $this->_html .= '<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
            <fieldset>
                <legend><img src="../modules/scroller/logo.gif" alt="" class="middle" />'.$this->l('Settings').'</legend>
                <br />
                <center><input type="submit" name="submit" value="'.$this->l('Upgrade').'" class="button" /></center>
            </fieldset> 
        </form>';
    }
}

在上面的代码中应该添加mymodule.tpl模板,其中包含主要的html代码,即

return $this->display(__FILE__, 'mymodule.tpl');

另外,在 mymodule 中的哪个位置.php添加在 head tag 中链接的 js 和 css 文件,如下所示:

public function hookHeader()
{
    Tools::addJS($this->_path.'js/myjscript1.js');
    Tools::addJS($this->_path.'js/myjscript2.js');
    Tools::addCSS($this->_path.'css/mymodule.css', 'all');
}

如果上面的代码需要,应该在哪里添加global $smarty;

应在 install 方法中声明hookHeader

您应该在需要使用 $smarty 的每个类方法的开头引用global $smarty

这意味着,在使用$this->display()呈现 .tpl 的方法中,还应添加global $smarty以便能够使用 $smarty->assign() 方法。

函数 hookHome() 和 hookFooter() 返回的任何内容都将在钩子点渲染时显示。一开始忽略聪明可以做这样的事情:

public function hookHome($params)
{
  return "<h2>Wow, my module displays something</h2>";
}

当然,您可能希望在模块中使用模板文件 - 尽管您不必这样做。如果你打算使用一个聪明的模板,那么你可以在钩子函数中声明一个全局实例。

public function hookHome($params)
{
  global $smarty;
  ...
  ...

正如 Mihai 的回答中所述,如果您想使用 "header" 钩子插入 css 和 js,您还必须修改安装功能:

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