PHP code example of mvenghaus / magento2-widget-directive

1. Go to this page and download the library: Download mvenghaus/magento2-widget-directive library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mvenghaus / magento2-widget-directive example snippets


$content = <<<EOF
Lorem ipsum dolor sit amet, consetetur sadipscing elitr.

{{widget type="TestWidget\Widget" param1="foo" param2="bar"}}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr.
EOF;


$widgetParser = new \Mvenghaus\Magento2WidgetDirective\WidgetParser();
$widgets = $widgetParser->parse($content);

/** @var \Mvenghaus\Magento2WidgetDirective\Data\WidgetData $widget */
foreach ($widgets as $widget) {
    echo $widget->type;
    print_r($widget->properties);
}

$widgetBuilder = new \Mvenghaus\Magento2WidgetDirective\WidgetBuilder();

$result = $widgetBuilder->build(
    new WidgetData(
       type: '\\TestWidget\\Widget',
       properties: [
           'param1' => 'foo',
           'param2' => 'bar'
        ]
    )
);

// -> {{widget type="TestWidget\Widget" param1="foo" param2="bar"}}