Scandi CMS System Overview
An overview of how the Scandi CMS system works with widgets.
The Widget System
<p>Some CMS text...</p>
<p>{{widget type="Magento\\Catalog\\Block\\Widget\\RecentlyViewed" uiComponent="widget_recently_viewed" page_size="5" show_attributes="name,image,price,learn_more" show_buttons="add_to_cart" template="product/widget/viewed/grid.phtml"}}</p><p>Some CMS text...</p>
<p>
<widget type='RecentlyViewed' uiComponent='widget_recently_viewed' page_size='5'
show_attributes='name,image,price,learn_more' show_buttons='add_to_cart'></widget>
</p>The Html Component
Html Componentimport parser from 'html-react-parser';
import attributesToProps from 'html-react-parser/lib/attributes-to-props';
import domToReact from 'html-react-parser/lib/dom-to-react';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import Image from 'Component/Image';
import Link from 'Component/Link';
import WidgetFactory from 'Component/WidgetFactory';
import { hash } from 'Util/Request/Hash';
/** @namespace Component/Html/Component */
export class Html extends PureComponent {
static propTypes = {
content: PropTypes.string.isRequired
};
rules = [
{
query: { name: ['widget'] },
replace: this.replaceWidget
},
{
query: { name: ['a'] },
replace: this.replaceLinks
},
{
query: { name: ['img'] },
replace: this.replaceImages
} // [...]
];
parserOptions = {
replace: (domNode) => {
// [...] logic for replacing elements with components
}
};
attributesToProps(attribs) {...}
replaceLinks({ attribs, children }) {
const { href, ...attrs } = attribs;
return (
<Link { ...attributesToProps({ ...attrs, to: href }) }>
{ domToReact(children, this.parserOptions) }
</Link>
);
}
replaceImages({ attribs }) {
const attributes = attributesToProps(attribs);
if (attribs.src) {
return <Image { ...attributes } />;
}
}
replaceWidget({ attribs }) {
return <WidgetFactory { ...this.attributesToProps(attribs) } />;
}
render() {
const { content } = this.props;
return parser(content, this.parserOptions);
}
}The WidgetFactory Component
WidgetFactory ComponentLast updated