変数をCMSブロックに渡す
-
12-12-2019 - |
質問
Layout XMLを介してページに追加されるCMSブロックを持っています。
<block type="cms/block" name="my_cms_block" as="my_cms_block">
<action method="setBlockId">
<block_id>my_cms_block</block_id>
</action>
</block>
.
次のスニペットのページで使用されます。
<?php echo $this->getChildHtml('my_cms_block'); ?>
.
今すぐ希望するものは、{{var something}}
を介してCMSブロック自体で使用できるページからこのブロックに変数を渡すことができます。
例えば、製品ページの選択を介して使用したいCMSブロックを使用していますが、このブロックでは、製品属性と言う製品属性とは何かを使用できるようになりたいです。
解決
私はブロックに変数を渡すための解決策を持っていませんが、カスタム{{...}}
ディレクティブを含む他の解決策を持っています。
これはあなたの本当のシナリオであなたが必要とするものを達成するための方法です。
これをブロックコンテンツに入れる必要があります
{{attribute attribute="name"}}
.
name
を変更する必要があるものに変更します。
attribute
ディレクティブを作成する必要があります。基本的にディレクティブ名として何も使用できますが、大文字なしで1つの作業であることを確認できます。そのため、{{attr}}
はOK {{productAttr}}
が機能しません。
CMSブロックはMage_Widget_Model_Template_Filter
を使用して解析されるため、そのクラスを書き換えて新しいメソッドを追加する必要があります。
public function attributeDirective($construction) //handles {{attribute ....}}
{
if (!isset($construction[2])) { //if no parameters are passed ({{attribute}}) do nothing
return '';
}
if (!Mage::registry('current_product')) { //if not on product context
return '';
}
$params = $this->_getIncludeParameters($construction[2]);
if (!isset($params['attribute'])) { //if no attribute code is passed
return '';
}
$attributeCode = $params['attribute']; //get the attribute code
$product = Mage::registry('current_product'); //get the current product
$value = $product->getData($attributeCode); //get the value of the attribute
/** @var Mage_Catalog_Helper_Output $helper */
$helper = Mage::helper('catalog/output'); //get the output helper. The attribute might allow HTML so that needs to be parsed also.
return $helper->productAttribute($product, $value, $attributeCode); //return the processed attribute value
}
. 所属していません magento.stackexchange