Jump to content

Problemy z renderowaniem zmiennych w customowym module


Recommended Posts

Cześć

Mam problem z renderowaniem zawartości w moim customowym module. Całość odbywa się w panelu admina. O ile sam moduł się wgrywa i pojawia się nowa zakładka, o tyle zmienne wydają się nie być przekazywane do widoku.

Struktura modułu:

image.png.6ca665ed49e0166b69c9d4156ffb1871.png

 

modules/imagemanager/imagemanager.php

<?php
if (!defined('_PS_VERSION_')) {
    exit;
}

class ImageManager extends Module
{
    public function __construct()
    {
        $this->name = 'imagemanager';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'Test Test';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Image Manager');
        $this->description = $this->l('Simple Image Manager module.');
    }

    public function install()
    {
        return parent::install() && $this->registerTab();
    }

    public function uninstall()
    {
        return parent::uninstall() && $this->unregisterTab();
    }

    private function registerTab()
    {
        $tab = new Tab();
        $tab->class_name = 'AdminImageManager';
        $tab->id_parent = (int)Tab::getIdFromClassName('AdminParentModulesSf');
        $tab->module = $this->name;
        $tab->name = [];
        foreach (Language::getLanguages() as $lang) {
            $tab->name[$lang['id_lang']] = 'Image Manager';
        }
        return $tab->add();
    }

    private function unregisterTab()
    {
        $id_tab = (int)Tab::getIdFromClassName('AdminImageManager');
        if ($id_tab) {
            $tab = new Tab($id_tab);
            return $tab->delete();
        }
        return true;
    }
}

 

modules/imagemanager/controllers/admin/AdminImageManagerController.php

<?php

class AdminImageManagerController extends ModuleAdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->bootstrap = true;
    }

    public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign([
            'message' => 'test',
            'current_date' => date('Y-m-d H:i:s')
        ]);

        $templatePath = _PS_MODULE_DIR_ . 'imagemanager/views/templates/admin/imagemanager.tpl';

        if (!file_exists($templatePath)) {
            die('<h1 style="color:red;">BŁĄD: Plik szablonu nie istnieje: ' . $templatePath . '</h1>');
        }

        return $this->setTemplate('imagemanager.tpl');
    }
}

 

modules/imagemanager/views/templates/admin/imagemanager.tpl

{extends file="helpers/view/view.tpl"}

{block name="content"}
    <div class="panel">
        <h2>{l s='ImageMANAGER' mod='imagemanager'}</h2>
        <p>This is content from a .tpl file.</p>
    </div>
{/block}

 

Tak się to objawia:

image.thumb.png.6bc4cba61fdbb5322e71e29bae921688.png

Kiedy $this->setTemplate('imagemanager.tpl'); zamienię na echo '<h1>TEST TEST</h1>';

image.thumb.png.86401a0668c08fbdabb93ea46b8c1e1a.png

 

inny przykład testu echo '<pre>'; print_r($this->context->smarty->getTemplateVars()); echo '</pre>'; exit;

 [current_tab_level] => 2
    [install_dir_exists] => 
    [pic_dir] => /upload/
    [controller_name] => AdminImageManager
    [currentIndex] => index.php?controller=AdminImageManager
    [bootstrap] => 1
    [default_language] => 1
    [content] => 
    [message] => test
    [current_date] => 2025-02-28 22:20:29

 

zamiana na: $this->setTemplate('module:imagemanager/views/templates/admin/imagemanager.tpl'); dostaję taki błąd
image.thumb.png.92cb9ed4de63412475ec0aeecef426d3.png

Nie wiem w czym jest problem. Kiedyś pisałem inne moduły w podobnym stylu i ten problem się nie pojawiał.

Środowisko: Docker - windows 10

wersja Presta: 8.2.0

Link to comment
Share on other sites

Mały update:

imagemaneger/controllers/admin/AdminImageManagerController.php
 

<?php

class AdminImageManagerController extends ModuleAdminController
{
    public function __construct()
    {
        parent::__construct();
        $this->bootstrap = true;
    }

    public function initContent()
    {
        parent::initContent();

        $this->content .= '<h1 style="color: red;">TEST</h1>';

        $templatePath = _PS_MODULE_DIR_ . 'imagemanager/views/templates/admin/imagemanager.tpl';

        if (file_exists($templatePath)) {
            $this->content .= $this->context->smarty->fetch($templatePath);
        } else {
            $this->content .= '<p style="color: red;">BŁĄD: Plik `.tpl` nie istnieje</p>';
        }
    }
}

Sprawia, ze strona się ładuje, ale nic nie wyświetla:

image.png.f54e6560a919147bbf13eb5df9c5fc36.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...