0

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>';
    }
}

上記のコードのどこに、メインの html コードを含む mymodule.tpl テンプレートを追加する必要があります。

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

また、mymodule.php のどの場所に、次のように head タグでリンクされている 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;上記のコードで必要な場合は、どこに追加する必要がありますか?

4

2 に答える 2

0

はメソッドhookHeaderで宣言する必要があります。install

global $smartyを使用する必要がある各クラスメソッドの先頭で参照する必要があります$smarty

つまり$this->display()、.tplをレンダリングするために使用するメソッドでglobal $smartyは、メソッドを使用できるようにするためにも追加する必要があります$smarty->assign()

于 2012-06-23T23:18:34.363 に答える
0

関数 hookHome() および hookFooter() によって返されるものはすべて、フック ポイントがレンダリングされるときに表示されます。そもそも smarty を無視すると、次のようなことができます。

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

もちろん、モジュールでテンプレート ファイルを使用したい場合もありますが、必須ではありません。smarty テンプレートを使用する場合は、フック関数内でグローバル インスタンスを宣言できます。

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

Mihai の回答に記載されているように、「ヘッダー」フックを使用して css と js を挿入する場合は、インストール機能も変更する必要があります。

function install()
{
    if (!parent::install()
        OR !$this->registerHook('home')
        OR !$this->registerHook('footer')
        OR !$this->registerHook('header')
    )
        return false;
    return true;
}
于 2012-06-25T22:24:44.620 に答える