سؤال

لدي كتلة cms تمت إضافتها إلى الصفحة عبر تخطيط XML.

<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'); ?>

الآن ما أود فعله هو أن أكون قادرًا على تمرير المتغيرات إلى هذه الكتلة من الصفحة التي يمكن استخدامها في كتلة cms نفسها عبر {{var something}}

على سبيل المثال، لدينا كتلة cms التي نريد استخدامها عبر مجموعة مختارة من صفحات المنتج ولكن في هذه الكتلة نريد أن نكون قادرين على استخدام سمة المنتج مثل الاسم أو أي شيء حقيقي.

هل كانت مفيدة؟

المحلول

ليس لدي حل لتمرير المتغيرات إلى الكتلة، ولكن لدي حل آخر يتضمن التخصيص {{...}} التوجيهات.

إليك طريقة لتحقيق ما تحتاجه في السيناريو الحقيقي الخاص بك.

تحتاج إلى وضع هذا في محتوى الكتلة

{{attribute attribute="name"}}

يتغير name إلى أي رمز السمة الذي تحتاجه.

الآن أنت بحاجة إلى إنشاء attribute التوجيه.يمكنك بشكل أساسي استخدام أي شيء كاسم توجيه، لكن تأكد من أنه عمل واحد بدون أي أحرف كبيرة.لذا {{attr}} على ما يرام {{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
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top