【问题标题】:Add array element to smarty variable in PrestaShop 1.7 Theme在 PrestaShop 1.7 主题中将数组元素添加到 smarty 变量
【发布时间】:2020-05-11 00:02:26
【问题描述】:

在修改我的主题时,我想对这个模板文件进行更改

themes/my_theme/templates/catalog/_partials/miniatures/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 数组添加更多数据,以便在我的自定义商店中显示它。此数据来自自定义模块。

例如:

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

最好的方法是什么?

【问题讨论】:

    标签: prestashop smarty prestashop-1.7


    【解决方案1】:

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

    否则,您必须重写类别类,才能将信息添加到类别表中。

    问候

    【讨论】:

    • 所以我基本上会用我的自定义钩子简单地替换整个类别模板......? 似乎非常低效但有效。 PS 核心类将加载要在实际模板中使用的各种数据,但它永远不会显示,而是我在自定义挂钩中再次加载所有这些数据。
    • 您的 Hook 将允许您加载普通内容或个性化内容。在 tpl 级别,你的钩子应该只干预变量和自定义部分。
    【解决方案2】:

    这样做的方法是:

    1. 创建一个新模块
    2. 安装自定义挂钩
    3. 调用.tpl中的自定义钩子进行修改
    4. 传递所需数据的参数
    5. 在自定义挂钩函数中获取该数据,根据需要添加到其中
    6. 然后改为加载自定义 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_theme/templates/catalog/_partials/miniatures/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>
    

    同样,可能需要将原始 .tpl 文件的所有内容复制到此处,具体取决于您要查找的修改。

    【讨论】:

      【解决方案3】:

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

      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 分支

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 2019-04-12
        • 2017-09-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多