在PrestaShop的哪里注册全局smarty修饰符? [英] Where to register global smarty modifier in PrestaShop?

查看:114
本文介绍了在PrestaShop的哪里注册全局smarty修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基于PrestaShop(v 1.6)的商店,我想注册我的自定义修饰符插件,以便可以从任何模板(包括前台模板和后台模板)进行访问.

I'm creating a store based on PrestaShop (v 1.6), and I want to register my custom modifier plugin to be accessible from any templates (includinf front and back-officetemplates).

问题是在哪里放置注册码?

现在,我使用 Tools override 添加功能来处理修饰符(对于商店特定功能afaik,这是可接受的做法),并且 smarty.config.inc.php 来注册插件(因为所有PrestaShop插件都在此处注册),但是此文件包含有关请勿直接修改文件"的警告,据我所知,当该文件被覆盖时,我升级了PrestaShop.

Now I use a Tools class override to add function to handle modifier (that is acceptable practice in case of store-specific functionality, afaik), and smarty.config.inc.php to register plugin (because all PrestaShop plugins are registereg here), but this file contains a warning about "not to modify file directly" and, as far as I understand, will be overwritten when I upgrade PrestaShop.

所以,问题是在哪里注册我的插件,以确保我的代码不会被覆盖?

So, the question is where to register my plugin to be sure that my code will not be overwritten?

谢谢.

推荐答案

您可以使用模块来完成此操作.

You can do this with a module.

1.创建一个模块

在内部模块文件夹中创建一个文件夹testmodule,在内部模块中创建一个php文件testmodule.php.

Inside modules folder create a folder testmodule and inside create a php file testmodule.php.

我们将使用钩子actionDispatcher,该钩子在每个页面控制器实例化之后执行,以将修饰符插件注册到smarty中.

We will use a hook actionDispatcher which executes after every page controller instantiation to register the modifier plugin into smarty.

require_once _PS_MODULE_DIR_ . 'testmodule' . DIRECTORY_SEPARATOR . 'TestClass.php';

class TestModule extends Module {
    public function __construct()
    {
        $this->name = 'testmodule';
        $this->tab = 'front_office_features';
        $this->version = '1.0';

        parent::__construct();

        $this->displayName = $this->l('Test Module');
        $this->description = $this->l('Testing smarty plugins.');
    }

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

    public function hookActionDispatcher()
    {
        /* 
           We register the plugin everytime a controller is instantiated

           'modifier'                          - modifier type of plugin
           'testToUpper'                       - plugin tag name to be used in templates,
           array('TestClass', 'toUpperMethod') - execute toUpperMethod() from class TestClass when using modifier tag name
        */
        $this->context->smarty->registerPlugin('modifier', 'testToUpper', array('TestClass', 'toUpperMethod'));
    }
}

2.创建一个包含修饰符方法的类

在同一模块文件夹中创建文件TestClass.php. 在其中,我们将编写一个静态方法以在调用smarty插件时执行.对于这个简单的测试,我们将修改我们想要大写的任何字符串.

In the same module folder create a file TestClass.php. In it we will write a static method to execute when smarty plugin is called. For this simple test we will modify any string we want to uppercase.

class TestClass {
    public static function toUpperMethod($param)
    {
        return strtoupper($param);
    }
}

安装模块,您就可以在任何模板中使用您的插件,例如在首页上

Install the module and you can use your plugin in any template for example on front pages

{$page_name|testToUpper} 

将回显页面名称并将其转换为大写.

will echo and transform page name to uppercase.

例如,如果您尝试在数组上使用修饰符,则可以进行修改或保护,但这是注册smarty插件的基础.

You could make modifications or safeguards in case you try to use modifier on arrays for example but this is the basics of registering smarty plugins.

无需任何替代,也无需进行核心黑客入侵.

No overrides and no core hacking required.

这篇关于在PrestaShop的哪里注册全局smarty修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆