Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Prestashop中全局访问变量_Prestashop_Prestashop 1.6_Prestashop 1.5_Prestashop 1.7 - Fatal编程技术网

如何在Prestashop中全局访问变量

如何在Prestashop中全局访问变量,prestashop,prestashop-1.6,prestashop-1.5,prestashop-1.7,Prestashop,Prestashop 1.6,Prestashop 1.5,Prestashop 1.7,我正在修改Prestashop中的一个模块,它的文件夹结构如下 css translations index.php module.php module.tpl 有一个变量$products,它包含所有产品的数组。但它只能访问module.tpl,它在主页上显示我不喜欢的所有产品 我创建了一个重定向到不同页面的控制器和一个模板/view/front/products.tpl来显示所有产品。但是$products变量在products.tpl文件中没有定义。定义常量可以完成这项工作,也可以使用会

我正在修改Prestashop中的一个模块,它的文件夹结构如下

css
translations
index.php
module.php
module.tpl
有一个变量$products,它包含所有产品的数组。但它只能访问module.tpl,它在主页上显示我不喜欢的所有产品


我创建了一个重定向到不同页面的控制器和一个模板/view/front/products.tpl来显示所有产品。但是$products变量在products.tpl文件中没有定义。

定义常量可以完成这项工作,也可以使用会话。之后,smarty可以在模板中访问所有内容

模块中的Ex:

      define('MYGLOBALVAR', 'data');
在模板中:

      $smarty.const.MYGLOBALVAR

您可以设置Cookie变量并在需要的地方使用它。比如:

$this->context->cookie->products=$products;
若要在tpl中打印,可在控制器中分配给smarty变量

 $this->context->smarty->assign('products', $this->context->cookie->products);

在tpl中使用它

如果您已使用新模板创建了新控制器,则需要在此控制器中创建此变量并将其分配给模板,因此:

在控制器的
initContent()
函数中,您需要使用所需的值创建变量“$products”,例如:

$products = Product::getProducts($id_lang, 0, 0, 'id_product', 'DESC' );
然后,需要将此php变量分配给Smarty变量,以在tpl文件中显示该值。为此,我们使用“@Ravinder Pal”使用的方法,但更改了值:

$this->context->smarty->assign('products', $products);
最后,您可以在模板中使用此变量,该模板在initContent()函数中指定,如下所示:

{$products}

希望对你有帮助。

我真的弄明白了。我别无选择,只能实例化新类别,然后获取所有产品。 像这样:

$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$nb = 10000;    
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));

嘿,谢谢你的快速回复,虽然它并没有真正起作用。顺便说一句,我想知道是否有一种方法可以返回多个显示器,而不仅仅是一个。例如,hookDisplay($params){return$this->display(file.tpl)和$this->display(file2.tpl);}您可以使用fetch()函数。但是,如果在smarty之前加载,则上述方法有效。