0

I want to show this table on the frontend Product View page

How to replace {{custom_product_attribute_1}} with actual product attribute value. and these attributes can be dynamic.

Custom RAW HTML Template -

<table style="border-collapse: collapse; width: 100%;" border="1">
    <tbody>
        <tr>
            <td style="width: 16.6667%;">{{custom_product_attribute_1}}</td>
            <td style="width: 16.6667%;">{{custom_product_attribute_2}}</td>
            <td style="width: 16.6667%;">{{custom_product_attribute_3}}</td>
            ........
        </tr>
    </tbody>
</table>

Expected Result:-

<table style="border-collapse: collapse; width: 100%;" border="1">
    <tbody>
        <tr>
            <td style="width: 16.6667%;">10</td>
            <td style="width: 16.6667%;">15</td>
            <td style="width: 16.6667%;">30</td>
            ........
        </tr>
    </tbody>
</table>

I am using Admin to create HTML templates.

enter image description here

0

Is there any particular reason why you need to use a HTML template?

Typically (unless creating a UI component with Knockout JS), you would use a PHTML template for that sort of thing.

template-name.phtml (example, although there are other ways to achieve the same thing)

<?php
$product = $block->getProduct();
$attributeOne = $product->getData('custom_product_attribute') ?> 
?>
<table style="border-collapse: collapse; width: 100%;" border="1">
    <tbody>
        <tr>
            <td style="width: 16.6667%;"><?= $attributeOne ?></td>
            ........
        </tr>
    </tbody>
</table>

In your XML, set the class as Magento\Catalog\Block\Product\View\Description which will give you access to the getProduct method. e.g.

        <block class="Magento\Catalog\Block\Product\View\Description" name="custom.template" template="Magento_Catalog::product/view/template-name.phtml" />
3
  • actually, I am using admin to create HTML Templates. one template can be associated with many products.... see the attached image in the above question. – Vishal Thakur Jun 20 at 13:28
  • That will not work...as I don't know what attribute is used in the template...so can't use this code --> $attributeOne = $product->getData('custom_product_attribute') ?> – Vishal Thakur Jun 21 at 6:55
  • Ah for use in admin; In that case you won't be able to use the PHP code above. But also in this case, if you wanted these attributes to be dynamic, you wouldn't be able to apply your logic directly in the HTML. Is there any reason why it needs to be editable in the HTML admin template? If these are dynamic attributes then surely would be easier to loop through an attribute set and render the values if present inside a PHTML file? – Zudian Jun 21 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.