Widget是一個更好的工具,能夠將內容插入或編輯到CMS頁面塊或頁面中。php
小部件是Magento 2配置中的強大功能。做爲商店管理員,您能夠利用小部件來改善生動界面下的店面。小部件容許顯示靜態信息或動態內容營銷。我想說明一些Magento小部件的實現,例如:html
自定義 HelloWorld 主題下 :建立etc/widget.xml
包含如下內容的文件緩存
<?xml version="1.0" ?> <widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:helloworld:Magento_Widget:etc/widget.xsd"> <widget class="Mageplaza\HelloWorld\Block\Widget\Posts" id="mageplaza_helloworld_posts"> <label>Blog Posts</label> <description>Posts</description> <parameters> <parameter name="posts" sort_order="10" visible="true" xsi:type="text"> <label>Custom Posts Label</label> </parameter> </parameters> </widget> </widgets>
自定義 HelloWorld 主題下 :文件: view/frontend/templates/widget/posts.phtml
frontend
<?php if($block->getData('posts')): ?> <h2 class='posts'><?php echo $block->getData('posts'); ?></h2> <p>This is sample widget. Perform your code here.</p> <?php endif; ?>
自定義 HelloWorld 主題下 :建立塊文件: Block/Widget/Posts.php
工具
<?php namespace Mageplaza\HelloWorld\Block\Widget; use Magento\Framework\View\Element\Template; use Magento\Widget\Block\BlockInterface; class Posts extends Template implements BlockInterface { protected $_template = "widget/posts.phtml"; }
去 admin panel > Content > Pages > Home page > Edit
post
在Content
選項卡中,單擊Insert Widget
圖標spa
您將看到Blog posts
小部件列表code
插入成功。orm