An overview of how the Scandi CMS system works with widgets.
First, let's get familiar with how the widget system works in Magento+Scandi. This will give you a high-level understanding on what bits of code need to be added to create a new widget.
Reading this section is optional, and only needed if you want to get a high-level understanding for the context of the code we'll be writing.
The Widget System
First, let's get familiar with how the widget system works in Magento+Scandi. As an example, suppose the Admin creates a home page CMS page with a RecentlyViewed widget. The CMS source code would contain a {{widget}} template:
On the frontend, Scandi will parse this element. Let's dive into the parsing logic.
The Html Component
In Scandi, the Html component is responsible for rendering all CMS content, including CMS pages and product descriptions. Instead of merely outputting the raw HTML, it parses its input. Then, some of the elements are replaced by React components. This means that CMS content can contain components just like any other part of the app!
Take a look at this simplified version of the HTML component:
Instead of rendering simple <a> anchor elements, the Html component replaces them with <Link> β making them work with the react-router. Instead of outputting <img> elements, it converts them into <Image> components, making them consistent with the rest of the app.
Importantly, all <widget> elements are turned into the <WidgetFactory> component. This is what enables Scandi to process the CMS widgets.
The WidgetFactory Component
In the WidgetFactory, different widget types are mapped to corresponding components. The renderContent function then renders the appropriate component for the widget, passing on all the attributes as props: