Magento 2 淘汰赛 XML->PHTML->JS->HTML
Magento 2 Knockout XML->PHTML->JS->HTML
我想在 html 结帐时在登录弹出窗口中显示静态块,但是有问题。
这是从js调用的html模板,这个js 是从 phtml 调用的,这个 phtml 模板是从 xml
布局。
( xml -> phtml -> js -> html)
所以问题是如何从 phtml 或 xml 发送自定义内容块js 到 html 模板
vendor/magento/module-catalog/view/frontend/layout/default.xml
此文件正在调用 pthml 模板
<block class="Magento\Customer\Block\Account\AuthenticationPopup" name="authentication-popup" as="authentication-popup" template="Magento_Customer::account/authentication-popup.phtml">
vendor/magento/module-customer/view/frontend/templates/account/authentication-popup.phtml
此文件正在调用 js 布局,代码为:
<script type="text/x-magento-init">
{
"#authenticationPopup": {
"Magento_Ui/js/core/app": <?= /* @noEscape */ $block->getJsLayout() ?>
}
}
</script>
vendor/magento/module-customer/view/frontend/web/js/view/authentication-popup.js
此文件称为最后一个 html 模板,其中应该是来自管理面板的 static 块,代码:
define([
'jquery',
'ko',
// ......... //
], function ($, ko, /* ... ... ... .... ... */) {
'use strict';
return Component.extend({
registerUrl: window.authenticationPopup.customerRegisterUrl,
forgotPasswordUrl: window.authenticationPopup.customerForgotPasswordUrl,
autocomplete: window.authenticationPopup.autocomplete,
modalWindow: null,
isLoading: ko.observable(false),
defaults: {
template: 'Magento_Customer/authentication-popup'
},
});
});
这就是我在 php
中获得此块的方式
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('reset_password_notice')->toHtml(); ?>
I tried to paste it to phtml, it doesn't work !!!
您需要将此代码放入您的 phtml 文件中。
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('reset_password_notice')->toHtml(); ?>
现在它会显示您写入此块的内容。
问题自己解决了。
因此,第一步我开始寻找数据提供者,它有助于将数据从 pthml 通过 js 发送到 html 在 vendor/module-customer/
在那里我找到了文件 vendor/module-customer/Model/Checkout/ConfigProvider.php。这正是我所需要的。
根据这个 link 我创建了:
1) app/code/Theme/Customer/etc/frontend/di.xml 代码:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Account\CreatePost"
type="Theme_Name\Customer\Controller\Account\CreatePost" />
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="cms_block_config_provider" xsi:type="object">Theme_Name\Customer\Model\Checkout\ConfigProvider</item>
</argument>
</arguments>
</type>
</config>
2) 下一步是创建一个在 item 标签中调用的 class:Theme_Name/Customer/Model/Checkout/ConfigProvider.php
使用扩展
的代码
vendor/module-customer/型号/结账/ConfigProvider.php
Note! They both implement the same ConfigProviderInterface. So in new ConifgProvider.php we use the same interface to extend data-provider correctly
<?php
namespace Theme_Name\Customer\Model\Checkout;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;
class ConfigProvider implements ConfigProviderInterface
{
/** @var LayoutInterface */
protected $_layout;
public function __construct(LayoutInterface $layout)
{
$this->_layout = $layout;
}
public function getConfig()
{
$cmsBlockId = 'block_ID'; // id of cms block to use
return [
'cms_block_message' => $this->_layout->createBlock('Magento\Cms\Block\Block')->setBlockId($cmsBlockId)->toHtml()
];
}
}
很好。提供商已配置。
3) 最后一个是需要覆盖前端 html KO 模板:
app/design/frontend/theme_name/Magento_Customer/web/template/authentication-popup.html
写下:
<div data-bind="html: window.checkoutConfig.cms_block_message"></div>
我想在 html 结帐时在登录弹出窗口中显示静态块,但是有问题。
这是从js调用的html模板,这个js 是从 phtml 调用的,这个 phtml 模板是从 xml 布局。 ( xml -> phtml -> js -> html)
所以问题是如何从 phtml 或 xml 发送自定义内容块js 到 html 模板
vendor/magento/module-catalog/view/frontend/layout/default.xml
此文件正在调用 pthml 模板
<block class="Magento\Customer\Block\Account\AuthenticationPopup" name="authentication-popup" as="authentication-popup" template="Magento_Customer::account/authentication-popup.phtml">
vendor/magento/module-customer/view/frontend/templates/account/authentication-popup.phtml
此文件正在调用 js 布局,代码为:
<script type="text/x-magento-init">
{
"#authenticationPopup": {
"Magento_Ui/js/core/app": <?= /* @noEscape */ $block->getJsLayout() ?>
}
}
</script>
vendor/magento/module-customer/view/frontend/web/js/view/authentication-popup.js
此文件称为最后一个 html 模板,其中应该是来自管理面板的 static 块,代码:
define([
'jquery',
'ko',
// ......... //
], function ($, ko, /* ... ... ... .... ... */) {
'use strict';
return Component.extend({
registerUrl: window.authenticationPopup.customerRegisterUrl,
forgotPasswordUrl: window.authenticationPopup.customerForgotPasswordUrl,
autocomplete: window.authenticationPopup.autocomplete,
modalWindow: null,
isLoading: ko.observable(false),
defaults: {
template: 'Magento_Customer/authentication-popup'
},
});
});
这就是我在 php
中获得此块的方式<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('reset_password_notice')->toHtml(); ?>
I tried to paste it to phtml, it doesn't work !!!
您需要将此代码放入您的 phtml 文件中。
<?php echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('reset_password_notice')->toHtml(); ?>
现在它会显示您写入此块的内容。
问题自己解决了。 因此,第一步我开始寻找数据提供者,它有助于将数据从 pthml 通过 js 发送到 html 在 vendor/module-customer/
在那里我找到了文件 vendor/module-customer/Model/Checkout/ConfigProvider.php。这正是我所需要的。
根据这个 link 我创建了:
1) app/code/Theme/Customer/etc/frontend/di.xml 代码:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Account\CreatePost"
type="Theme_Name\Customer\Controller\Account\CreatePost" />
<type name="Magento\Checkout\Model\CompositeConfigProvider">
<arguments>
<argument name="configProviders" xsi:type="array">
<item name="cms_block_config_provider" xsi:type="object">Theme_Name\Customer\Model\Checkout\ConfigProvider</item>
</argument>
</arguments>
</type>
</config>
2) 下一步是创建一个在 item 标签中调用的 class:Theme_Name/Customer/Model/Checkout/ConfigProvider.php
使用扩展
的代码
vendor/module-customer/型号/结账/ConfigProvider.php
Note! They both implement the same ConfigProviderInterface. So in new ConifgProvider.php we use the same interface to extend data-provider correctly
<?php
namespace Theme_Name\Customer\Model\Checkout;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\LayoutInterface;
class ConfigProvider implements ConfigProviderInterface
{
/** @var LayoutInterface */
protected $_layout;
public function __construct(LayoutInterface $layout)
{
$this->_layout = $layout;
}
public function getConfig()
{
$cmsBlockId = 'block_ID'; // id of cms block to use
return [
'cms_block_message' => $this->_layout->createBlock('Magento\Cms\Block\Block')->setBlockId($cmsBlockId)->toHtml()
];
}
}
很好。提供商已配置。
3) 最后一个是需要覆盖前端 html KO 模板:
app/design/frontend/theme_name/Magento_Customer/web/template/authentication-popup.html
写下:
<div data-bind="html: window.checkoutConfig.cms_block_message"></div>