Magento 1.9.x – How to Fix Text Area Attribute Displaying Code Instead of Rendering

attributesblockshtmlphtml

Live Demo: https://www.tugasunwear.com/sc-tuga-test#horizontalTab4

Goal: Use existing magento short code (like below) to display a .phtml page in the app/design folder

Code in product attribute textarea:

{{block type="core/template" name="description" template="custom/fabric_care/01.phtml"}}

Code in .phtml file to be rendered:

Imported<br /><br />
<strong>Fabric</strong>:<br />80% Nylon&nbsp;/&nbsp;20% Spandex<br /><br />
<strong>Care</strong>:<br />Rinse, hand wash, drip dry, do not tumble dry/dry clean/iron, do not dry rolled up

Problem: Magento Short Code is being displayed instead of rendering the code in the .phtml page it is linking to.

Other Info: This attribute is a custom addition into the "Neighborhood" theme by Peerforest – who no longer provides support through themeforest on this theme. I wanted change it to a dropdown from a textarea, but that does not work properly.

This is the code rendering this custom attribute:

<?php $_product = $this->getProduct(); ?>
<?php if($_product->getCustomtext()): ?>
       <?php echo $_product->getCustomtext() ?>
<?php endif; ?>

This is the layout.xml rendering the custom attribute:

<action method="addTab" translate="title" module="catalog">
            <alias>customtext</alias>
            <title>Fabric &amp; Care</title>
            <block>catalog/product_view</block>
            <template>catalog/product/view/customtext.phtml</template>
        </action>

I also added this code change to the output.php file copied to my local code folder (as suggested by this post):

if ($attribute->getFrontendInput() != 'price') {
                  if($attributeName != 'customtext') {
                    $attributeHtml = $this->escapeHtml($attributeHtml);
                  }
                }

Attribute Properties:
enter image description here

Frontend Properties:
enter image description here

If there is more info needed, please let me know. Hopefully this isn't an impossible task.

Best Answer

The content of .phtml file is simply echoing out attribute value as it's saved.

For it to render the Magento block codes, you need to use the CMS page template processor on the content which will effectively render any block codes or variables saved on your attribute.

To do this, you can use the following code:

$cmsHelper = Mage::helper('cms');
$processor = $cmsHelper->getPageTemplateProcessor();
echo $processor->filter($yourContentVariable);  //replace $yourContentVariable with the content that contains the block codes.

For your specific example, the code would look like this:

<?php $_product = $this->getProduct(); ?>
<?php if($_product->getCustomtext()): ?>
<?php
 $cmsHelper = Mage::helper('cms');
 $processor = $cmsHelper->getPageTemplateProcessor();
 echo $processor->filter($_product->getCustomtext());
?>
<?php endif; ?>
Related Topic