0

I dont know how to convert below PHP code into twig. I am implementing this in Opencart 3.x

 $doc = new DOMDocument();
 $doc->loadHTML(mb_convert_encoding($blog['description'], 'HTML-ENTITIES', 'UTF-8'));
 $yourText = $doc->saveHTML();
 echo $yourText

Thank you in advance.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Radhika
  • 533
  • 5
  • 22
  • What do you want converting to Twig? – Nigel Ren Apr 18 '18 at 07:17
  • Possible duplicate of [How to convert PHP code to Twig code?](https://stackoverflow.com/questions/46539636/how-to-convert-php-code-to-twig-code) – Mad Coder Apr 18 '18 at 07:17
  • @NigelRen want to convert provided php code in my question – Radhika Apr 18 '18 at 07:18
  • you know twig actually converts template code to PHP, although the method they use for tokenization and lexing is pretty restricted. I will say it was innovative for it's time. – ArtisticPhoenix Apr 18 '18 at 07:19
  • 4
    You don't "convert" PHP to twig. You need to build a twig function (with your PHP code) that can be called from twig. https://twig.symfony.com/doc/2.x/advanced.html#functions – M. Eriksson Apr 18 '18 at 07:19
  • 1
    @MadCoder - If you look at that question and it's answer, it doesn't really apply here. – M. Eriksson Apr 18 '18 at 07:23
  • 4
    You can also call this code in your controller and just pass the `$yourtext` variable to twig where you can output it using the `raw` filter if it is html. – jeroen Apr 18 '18 at 07:27

1 Answers1

0

The first three lines of your code belong in a controller file. I have no idea where you're putting this so you need to figure out which controller file to use for that corresponds to the twig file template it should appear in.

So this part goes into whatever controller file you're using.

 $doc = new DOMDocument();
 $doc->loadHTML(mb_convert_encoding($blog['description'], 'HTML-ENTITIES', 'UTF-8'));
 $yourText = $doc->saveHTML();

Next, you need to find your corresponding view template (.twig) file and you can echo $yourText by including where ever it's supposed to be with:

{{ yourText }}
Nancy
  • 522
  • 4
  • 15