I thought this would be better in Magento 2, but currently (Magento 2.1.4) Magento still has a quote complex way of rendering a product list. I'm talking about vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
. This is the template that is responsible for rendering the catalog- and the search results list.
The task
We all had the task at some point where the client requests a small addition to this template. Perhaps a product label, or an additional image, etc. But this template has very few points to hook into. The way I currently see it, when you want to make a modification to this list there are the following options:
The posible options
- Copy/paste the
phtml
-file to your design folder and make the adjustments there. This - of course - is no option if you want to modify this template from within your module. - Add a modified template to your own module and point Magento to it from within your own custom
catalog_category_view.xml
. However, this could give unexpected results when using 3rd party templates. And I generally don't like the idea to overrule such a big template purely for a minor edit. - Rewrite the
default
product details renderer. This however gives the problem that as soon as you have a configurable product the default renderer is no longer rendered. - Hook into the
addto
-block (since this is the only place in the template wheregetChildHtml()
is called for dynamicly added child templates. - Use JavaScript to enrich the html later on. This might seem a bit like the wrong solution for the problem, especially when what you're trying to render can already be done server-side.
- There are some other hacky options (like using plugins to modify the output with regexp), but those are so not-done that I won't even discuss them here.
The question
So with the above options that I currently can think of, what would be the best approach to add custom html the the product block (in the product list). I'm currently tending toward the 'addto'-method, although it also seems like it's not the exact right place for the job. But at least it allows me to:
- Minimize the changes I have to do about core templates.
- Maximize the compatibility with external templates.
- Keeps my own logic separated.
Of course it would be nicer if the rendering of a single product provided more hooks to manipulate the output (or at least, don't wrap the whole thing inside one massive foreach
-loop inside list.phtml
). Perhaps there are plans for this in the future?
What are your suggestions on this task?