简体   繁体   English

Magento 2 - 如何有 2 个单独的 phtml 来处理产品列表和产品网格?

[英]Magento 2 - How to have 2 separates phtml to handle products list and products grid?

I'm building a new website based on Magento 2. I'm still learning this CMS and I'm wondering one thing.我正在建立一个基于 Magento 2 的新网站。我还在学习这个 CMS,我想知道一件事。 Based on the webdesigner's work I've too many differences between the grid layout of the product list and the list one.基于网页设计师的工作,我在产品列表的网格布局和列表一之间有太多差异。

For now, to style the product grid, I've duplicate and edit this file : app/design/frontend/MYTHEME/default/Magento_Catalog/templates/product/list.phtml现在,为了设计产品网格,我复制并编辑了这个文件: app/design/frontend/MYTHEME/default/Magento_Catalog/templates/product/list.phtml

But it handle both grid and list layout, of course :但它当然处理网格和列表布局:

<div class="products wrapper <?= /* @noEscape */ $viewMode ?> products-<?= /* @noEscape */ $viewMode ?>">

I need to move too many things for the list layout so I try to have 2 separate phtml, one for the list layout and one for the grid layout.我需要为列表布局移动太多东西,所以我尝试使用 2 个单独的 phtml,一个用于列表布局,另一个用于网格布局。 Is it possible, for example defining things in app/design/frontend/MYTHEME/default/Magento_Catalog/layout/catalog_category_view.xml I guess ?是否有可能,例如在app/design/frontend/MYTHEME/default/Magento_Catalog/layout/catalog_category_view.xml定义东西我猜?

Thanks for your help.谢谢你的帮助。

In your phtml file:在您的 phtml 文件中:

app/design/frontend/MYTHEME/default/Magento_Catalog/templates/product/list.phtml应用程序/设计/前端/MYTHEME/default/Magento_Catalog/templates/product/list.phtml

Why don't you just do something like this:你为什么不做这样的事情:

<?php if ($viewMode == 'grid') :?>
    // Grid layout
<?php else :?>
    // List layout
<?php endif; ?>

If you want to separate , you will have find out the Block Class which called this template ( list.phtml ).如果你想分开,你会找到调用这个模板的块类(list.phtml)。

There, you will see setTemplate function (some blocks will not have because its already in parent class).在那里,您将看到setTemplate函数(有些块没有,因为它已经在父类中)。

You will get viewMode there too, so you will have to set a condition like below:您也会在那里获得 viewMode,因此您必须设置如下条件:

if ($viewMode == 'grid') : 
   $this->setTemplate = '...../grid.phtml';
else : 
   $this->setTemplate = '...../list.phtml';

inside the setTemplate Function.setTemplate函数中。

I think you already know where and how to create grid phtml file.我想您已经知道在哪里以及如何创建网格phtml 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM