0

I have mention below template file for faq.But it throws all html tags in frontend.How to remove this? If It is correct way use escapeHtml in template file.Is it neccessary or not?

<?php
    $collectionTitle = $this->getCollectionTitle();
    ?>
    <div id="element" data-mage-init='{"accordion":{"openedState": "active", "collapsible": true, "active": false, "multipleCollapsible": true}}'> 
        <?php foreach ($collectionTitle as $item): ?>  
            <div data-role="collapsible">
                <div data-role="trigger"> 
                    <span>
                        <?php echo $block->escapeHtml($item->getCategoryName()); ?>
                    </span>
                </div>
            </div>    

            <?php $collection = $this->getCollection($item->getId()); ?> 
            <div data-role="content">
                <?php foreach ($collection as $items): ?>             
                    <?php echo $block->escapeHtml($items->getQuestion()); ?> 
                    <?php echo $block->escapeHtml($items->getAnswer()); ?>            
                <?php endforeach; ?> 
            </div>
        <?php endforeach; ?>
</div>   

Layout file

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>FAQs</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Xxx\Faqs\Block\Faq\Collection" name="footerLink" template="Xxx_Faqs::view.phtml">
                <container name="form.additional.info" label="Form Additional Info"/>
            </block>
        </referenceContainer>
    </body>
</page>
4
  • create a new phtml file and try ti again
    – Amit Bera
    Nov 29 '17 at 13:14
  • Post your layout code! Nov 29 '17 at 13:15
  • updated my post
    – Mahi M
    Nov 29 '17 at 13:22
  • nothing change it @Amit Bera
    – Mahi M
    Nov 29 '17 at 13:26
0

di.xml

<type name="Vendor\Faq\Block\Faq\Collection"> <!-- change name here to match your block class name -->
        <arguments>
            <argument name="templateProcessor" xsi:type="object">Magento\Widget\Model\Template\Filter</argument>
        </arguments>
</type>

In Vendor\Faq\Block\Faq\Collection.php define filterOutputHtml() function like below :

   public function __construct(
    ....
        \Zend_Filter_Interface $templateProcessor,
        array $data = array()
    ) {    
    ...
        $this->templateProcessor = $templateProcessor;
        parent::__construct($context, $data);

    }
   public function filterOutputHtml($string) 
    {
        return $this->templateProcessor->filter($string);
    }

use filterOutputHtml() function in template file

<?php
    $collectionTitle = $this->getCollectionTitle();
    ?>
    <div id="element" data-mage-init='{"accordion":{"openedState": "active", "collapsible": true, "active": false, "multipleCollapsible": true}}'> 
        <?php foreach ($collectionTitle as $item): ?>  
            <div data-role="collapsible">
                <div data-role="trigger"> 
                    <span>
                        <?php echo $block->filterOutputHtml($item->getCategoryName()); ?>
                    </span>
                </div>
            </div>    

            <?php $collection = $this->getCollection($item->getId()); ?> 
            <div data-role="content">
                <?php foreach ($collection as $items): ?>             
                    <?php echo $block->filterOutputHtml($items->getQuestion()); ?> 
                    <?php echo $block->filterOutputHtml($items->getAnswer()); ?>            
                <?php endforeach; ?> 
            </div>
        <?php endforeach; ?>
</div>   
2
  • working thanks.But why escapeHtml concept is not working here
    – Mahi M
    Nov 30 '17 at 7:24
  • escapeHtml Use in the case of a string output that should not contain HTML.it is not convert into html
    – mansi
    Nov 30 '17 at 8:07

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.