在 magento 2 中添加带有 ajax 渲染块的子块
Add child block with ajax rendered block in magento 2
我已经用 ajax 渲染了注册表单。以下是呈现注册表单的函数,
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_Customer::form/register.phtml')
//->toHtml()
;
但是从管理员创建的这个其他属性不会显示在表单上。
所以为了显示额外的文件,我用下面的代码更新了上面的代码,
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_CustomerCustomAttributes::/customer/form/register.phtml')
//->toHtml()
;
但是属性还是不显示,有大神知道吗?
问题已通过添加更新句柄得到解决。
if ($this->getRequest()->isAjax()) {
try {
$resultPage=$resultPage->addHandle('customer_form_template_handle');
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_CustomerCustomAttributes::/customer/form/register.phtml')
;
$block_attr = $resultPage->getLayout()
->createBlock('Magento\CustomerCustomAttributes\Block\Form')
->setTemplate('Magento_CustomerCustomAttributes::form/userattributes.phtml')
->setFormCode('customer_account_create')
->setEntityModelClass('Magento\Customer\Model\Customer')
;
$block->setChild('customer_form_user_attributes',$block_attr);
return $this->getResponse()->setBody($block->toHtml());
} catch (\Exception $e) {
$response = $e->getMessage();
return $this->getResponse()->setBody($response);
}
}
我已经用 ajax 渲染了注册表单。以下是呈现注册表单的函数,
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_Customer::form/register.phtml')
//->toHtml()
;
但是从管理员创建的这个其他属性不会显示在表单上。 所以为了显示额外的文件,我用下面的代码更新了上面的代码,
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_CustomerCustomAttributes::/customer/form/register.phtml')
//->toHtml()
;
但是属性还是不显示,有大神知道吗?
问题已通过添加更新句柄得到解决。
if ($this->getRequest()->isAjax()) {
try {
$resultPage=$resultPage->addHandle('customer_form_template_handle');
$block = $resultPage->getLayout()
->createBlock('Magento\Customer\Block\Form\Register')
->setTemplate('Magento_CustomerCustomAttributes::/customer/form/register.phtml')
;
$block_attr = $resultPage->getLayout()
->createBlock('Magento\CustomerCustomAttributes\Block\Form')
->setTemplate('Magento_CustomerCustomAttributes::form/userattributes.phtml')
->setFormCode('customer_account_create')
->setEntityModelClass('Magento\Customer\Model\Customer')
;
$block->setChild('customer_form_user_attributes',$block_attr);
return $this->getResponse()->setBody($block->toHtml());
} catch (\Exception $e) {
$response = $e->getMessage();
return $this->getResponse()->setBody($response);
}
}