1

我在 shopware 5.2.24 上尝试示例程序“Slogan of the day”

但是代码不起作用。

在文件 Bootstrap.php 中,我定义了 3 个重要的函数。在函数安装中,将调用回调函数“onFrontendPostDispatch”。

public function install()
{
    $this->subscribeEvent(
        'Enlight_Controller_Action_PostDispatchSecure_Frontend',
        'onFrontendPostDispatch'
    );

    $this->createConfig();

    return true;
}

private function createConfig()
{
    $this->Form()->setElement(
        'select',
        'font-size',
        array(
            'label' => 'Font size',
            'store' => array(
                array(12, '12px'),
                array(18, '18px'),
                array(25, '25px')
            ),
            'value' => 12
        )
    );

    $this->Form()->setElement('boolean', 'italic', array(
        'value' => true,
        'label' => 'Italic'
    ));
}

在这个回调函数中,我定义了新 tpl 文件的参数和新 tpl 文件的位置:[ _ DIR _ 。'/视图']

public function onFrontendPostDispatch(Enlight_Event_EventArgs $args)
{
    /** @var \Enlight_Controller_Action $controller */
    $controller = $args->get('subject');
    $view = $controller->View();

    $view->addTemplateDir(
        __DIR__ . '/Views'
    );

    $view->assign('sloganSize', $this->Config()->get('font-size'));
    $view->assign('italic', $this->Config()->get('italic'));
    $view->assign('slogan', $this->getSlogan());

}

public function getSlogan()
{
    return array_rand(
        array_flip(
            array(
                'My Slogan Number 1',
                'My Slogan Number 2',
                'My Slogan Number 3',
            )
        )
    );
}

新的 tpl 文件是:

{extends file="parent:frontend/index/index.tpl"}

{block name="frontend_index_navigation_categories_top_include"}

<style>
    .slogan-box {
        width:100%;
        text-align:center;
    }
    .slogan {
        {if $italic}font-style:italic;{/if}
        font-size:{$sloganSize}px;
    }
</style>

<div class="slogan-box">
    <span class="slogan">{$slogan}</span>
</div>

{$smarty.block.parent}

{/block}

新的 tpl 文件的位置是:

在此处输入图像描述

但是在主页上,我看不到标语……它不起作用。

文件 Bootstrap.php 工作正常。但是首页上看不到标语。

Bootstrap.php和index.tpl之间的连接错了吗?

有谁知道我哪里出错了?非常感谢!!!

4

3 回答 3

0

看来,createConfig函数是未定义的。定义函数或注释掉函数$this->createConfig();内部的install()函数调用。
现在从后端插件管理器重新安装您的插件。它应该工作。

于 2017-06-20T05:36:26.050 回答
0

插件看起来不错,看不出有什么问题。

安装和激活插件后你清理模板缓存了吗?在后端manu: Configuration->Cache/Perfomance中,然后在Perfomance窗口中进入Che cache选项卡-> 全选-> 清除,然后确认主题编译。

或者您可以将下一个代码添加到插件 bootstrap SwagSloganOfTheDay/Bootstrap.php,这样当您尝试启用插件时,您会一直收到主题编译建议:

public function enable() {
    if(parent::enable())
        return array(
            'success' => true,
            'message' => 'enabled',
            'invalidateCache' => array('config', 'template', 'theme')
        );
}
于 2017-06-23T15:08:12.233 回答
0

您需要先注册您的模板。在订阅者/前端添加一个 php 文件:

/**
 * @inheritdoc
 */
public static function getSubscribedEvents()
{
    return [
        'Theme_Inheritance_Template_Directories_Collected' => 'onCollectTemplateDir'
    ];
}

    /**
     * @param \Enlight_Event_EventArgs $args
     *
     * Since 2.3.1 - added for template security
     */
    public function onCollectTemplateDir(\Enlight_Event_EventArgs $args)
    {
    
        $dirs = $args->getReturn();
        $dirs[] = $this->pluginDirectory . '/Resources/views';
        $args->setReturn($dirs);
    }
于 2022-02-22T12:11:37.070 回答