我需要将数据从.phtml中的块创建位置发送到我的CMS块。
我按照以下步骤在.phtml上创建代码块
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?>
<?php if ($category->getShortDescription()) : ?>
<?php echo $block->getLayout()->createBlock(
'Magento\Cms\Block\Block')->setBlockId('short_description')->setData('sd','Hello Short')->toHtml();?>
<?php endif; ?>
在这里,我以setData('sd','Hello Short')
的形式将参数传递给该块,但是该参数未显示在cms块上
我在博客中将此参数/参数称为
The Short Description is {{sd}}
但是我希望输出应该为The Short Description is Hello Short
,但是我为The Short Description is {{sd}}
答案 0 :(得分:0)
我已解决此问题,方法是替换CMS Block上的文本。
<?php if ($category->getShortDescription()) : ?>
<?php
$shortDescriptionBlock = $block->getLayout()->createBlock(
'Magento\Cms\Block\Block',"",["short_desc" => $category->getShortDescription()])->setBlockId('short_description')->toHtml();
echo str_replace("{{short_description}}", $category->getShortDescription(), $shortDescriptionBlock);?>
<?php endif; ?>