mvenghaus/magento2-widget-directive

Simple parser & build for Magento 2 widget directives outside of the Magento environment.

1.1.0 2024-10-14 06:02 UTC

This package is auto-updated.

Last update: 2024-11-14 06:10:45 UTC


README

Simple parser & builder for Magento 2 widget directives outside of the Magento environment.

Installation

Install the package via composer:

composer require mvenghaus/magento2-widget-directive:"^1.0"

Usage

Parse directives from a string

$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);
}

Building a directive

$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"}}

Run Tests

composer test