将数组元素添加到PrestaShop 1.7主题中的smarty变量

将数组元素添加到PrestaShop 1.7主题中的smarty变量,prestashop,smarty,prestashop-1.7,Prestashop,Smarty,Prestashop 1.7,修改主题时,我想更改此模板文件 themes/my\u theme/templates/catalog/\u partials/minimatures/Category.tpl 原始文件的内容: {block name='category_miniature_item'} <section class="category-miniature"> <a href="{$category.url}"> <img src="{$category.i

修改主题时,我想更改此模板文件

themes/my\u theme/templates/catalog/\u partials/minimatures/Category.tpl

原始文件的内容:

{block name='category_miniature_item'}
  <section class="category-miniature">
    <a href="{$category.url}">
      <img src="{$category.image.medium.url}" alt="{$category.image.legend}">
    </a>

    <h1 class="h2">
      <a href="{$category.url}">{$category.name}</a>
    </h1>

    <div class="category-description">{$category.description nofilter}</div>
  </section>
{/block}

最好的方法是什么?

最好的解决方案是添加一个模块并使用挂钩

否则,必须重写category类,以便将信息添加到category表中

要做到这一点,方法是:

  • 创建一个新模块
  • 安装自定义挂钩
  • 调用要修改的.tpl中的自定义挂钩
  • 传递所需数据的参数
  • 在自定义钩子函数中获取该数据,并根据需要添加到其中
  • 然后加载一个定制的tpl文件
  • 详细信息:

    安装自定义挂钩

    将此代码添加到主模块文件
    modules/my_module/my_module.php的安装函数中

    $custom_hook_name = 'displayMyContent';
    $new_hook = new Hook();
    $new_hook->name = pSQL($custom_hook_name);
    $new_hook->title = pSQL($custom_hook_name);
    $new_hook->add();
    
    调用.tpl中的钩子并传递参数

    {hook h='displayMyContent' category=$category}
    
    这是我们需要添加到要修改的原始.tpl文件中的代码(
    themes/my\u theme/templates/catalog/\u partials/minimatures/Category.tpl
    )。可能需要完全替换该文件中的整个代码

    在自定义挂钩函数中获取该数据

    返回
    modules/my_module/my_module.php
    并在模块中创建此函数:

    public function hookDisplayMyContent($params){      
    
        $category = $params['category']; //this is the parameter we passed in the .tpl file
    
        //do desired data modifications here
        $category['lowest_price'] = 0;
        $category['highest_price'] = 9000;
    
        $this->context->smarty->assign(array(
            'category' => $category
        ));
    
        return $this->display(dirname(__FILE__), 'views/templates/hook/category.tpl');
        //I have no idea why the display method has 2 parameters, but let's roll with it...
    }
    
    加载自定义tpl文件

    最后,我们可以在定制的.tpl文件
    modules/my_module/views/templates/hook/category.tpl中执行我们想要执行的任何操作

    例如:

        <h1 class="h2">
          <a href="{$category.url}">
              {$category.name} (from {$category.lowest_price} to {$category.highest_price} )
          </a>
        </h1>
    
    <h1 class="h2">
      <a href="{$category.url}">
          {$category.name} (from {$category.lowest_price} to {$category.highest_price} )
      </a>
    </h1>
    
    
    

    同样,可能需要在此处复制原始.tpl文件的所有内容,这取决于您要进行的修改。

    另一种可能更干净、更简单的方法是使用钩子

    hookDisplayOverrideTemplate

    然后可以添加任何模板变量

    public function hookDisplayOverrideTemplate($params){
    
        if($params['controller']->php_self == 'category'){
            $this->context->smarty->assign(array(
                'category_lowest_price' => 0,
                'category_highest_price' => 9000,
            ));
        }
    }
    
    然后所需的变量将可用于该模板中


    但是,这只适用于ThirtyBees,一个PrestaShop 1.6 fork,因此我基本上会用我的自定义挂钩替换整个类别模板。。。您的钩子将允许您加载普通内容或个性化内容。在tpl级别,钩子应该只干预变量和自定义部分。