簡體   English   中英

Magento-自定義擴展提供的塊未加載

[英]Magento - Block provided by custom extension is not loaded

我終於到達了第二個Magento模塊的“渲染”階段,但是我再次陷入困境。 現在,該模塊幾乎完成了,它具有一個Controller和兩個塊,其中一個塊被第一個調用(一個是儀表板塊,它使用第二個塊來填充子節)。

現在我需要做的是將這個新的儀表板添加到“客戶帳戶儀表板”中。 我被告知要使用布局文件,並且我已經在《 Magento設計指南》中閱讀了有關它們的信息。 然后,我創建了布局文件,並在其中添加了我的塊的聲明。 但是,儀表板沒有被修改,我的數據也沒有顯示。 如果我手動調用擴展方法(即myserver / customerstats / index),則會得到要顯示在“客戶帳戶”頁面上的HTML片段。

起初我以為我的布局文件沒有被加載,但是現在我有證據證明它已正確處理(至少是其中的一部分)。 這是布局文件:

<layout version="0.1.0">
    <customer_account_index translate="label">
        <label>My Custom Dashboard</label>
        <reference name="customer_account_dashboard">
            <!-- The action is processed correctly, the template is overridden -->
            <action method="setTemplate"><template>customerstats/customer/account/dashboard.phtml</template></action>
            <block type="customerstats/index" name="customerstats_dashboard" as="customerstats.dashboard" template="customerstats/customerstatsdashboard.phtml" />
        </reference>
    </customer_account_index>
</layout>

我將setTemplate操作添加到布局文件中,因為其中一項要求是將新的儀表板垂直分成兩部分。 這意味着我必須修改原始的customer / account / dashboard.phtml並更改頁面的結構,添加兩個將根據需要設置樣式的部分。 我不確定這是否是正確的方法,但是我不知道如何在不觸摸.phtml文件的情況下在頁面的特定部分添加其他內容。

我修改了新的dashboard.phtml ,以確保已加載它,並且啟用該操作后,我可以看到“客戶帳戶”頁面顯示了所做的更改。 但是,應該由我的擴展程序呈現的塊不會顯示在頁面上,並且基於我的簡單“陷阱”(一個簡單的“ die()”,以證明代碼已執行),擴展方法不會甚至似乎被稱為。

這是我要從塊中調用的控制器,已刪除了不相關的部分。

class MyCompany_CustomerStats_IndexController extends Mage_Core_Controller_Front_Action {
    // The loaded block is setting its own template in the _construct() phase
    $Block = &$this->getLayout()->createBlock('customerstats/customerstatsdashboard');

    // Some data is loaded elsewhere and passed to the Block

    $this->getResponse()->setBody(
        $Block->toHtml()
    );
}

如果我手動調用Controller(myserver / customerstats / index),它會正確觸發,並且它會返回完全呈現的HTML,但是,如開頭所述,當布局文​​件開始播放時,它似乎沒有運行。

總結一下,這是我的問題:
-關於覆蓋客戶帳戶信息中心,我是否走上了正確的軌道?
-是否必須在擴展程序的布局中指定模板屬性? 我實現的所有塊都是通過代碼(有一個邏輯來選擇要使用的模塊)加載模板的,並且我也看不到在xml文件中指定模板的用途。
-將塊添加到XML文件后,是否需要更改dashboard.phtml文件以將其呈現在某處? 我之所以問是因為我看到了原始的儀表板模板,並且其中包含對getChildHtml('alias name of a block')各種調用。 這使我認為,塊不必僅在布局文件中“暴露”,還可以在.phtml文件中手動調用。 但是,這是一種推測,因為我對這種機制沒有清晰的了解。 注意 :我還嘗試在新的dashboard.phtml中調用getChildHtml('customerstats.dashboard) ,但未進行任何更改。

在此先感謝您提供所有答案。

更新1-2012/07/28
這是未渲染的塊的代碼。

class MyCompany_CustomerStats_Block_UserStatsDashboard extends Mage_Core_Block_Template {
    const _TEMPLATE = 'customerStats/userstatsdashboard.phtml';

    public function _construct() {
        $this->setTemplate(self::_TEMPLATE);
    }
}

另外,我對控制器本身進行了實驗。 我將其更改如下:

class MyCompany_CustomerStats_IndexController extends Mage_Core_Controller_Front_Action {
    die('Controller called successfully.');
}

結果如下:-如果從瀏覽器中調用“ myserver / customerstats / index”,則會收到消息“控制器已成功調用”。 -如果加載修改后的儀表板頁面,則可以看到新的布局(這意味着我的儀表板已加載),但看不到該消息。 對我來說,這意味着Controller甚至沒有被調用。

Config.xml的內容

<?xml version="1.0"?>
<config>
    <modules>
        <MyCompany_CustomerStats>
            <version>0.1.0</version>
        </MyCompany_CustomerStats>
    </modules>
  <global>
    <helpers>
      <CustomerStats>
        <class>MyCompany_CustomerStats_Helper</class>
      </CustomerStats>
    </helpers>
    <models>
      <CustomerStats>
        <class>MyCompany_CustomerStats_Model</class>
      </CustomerStats>
    </models>
    <blocks>
      <CustomerStats>
        <class>MyCompany_CustomerStats_Block</class>
      </CustomerStats>
    </blocks>
  </global>

  <frontend>
    <routers>
      <CustomerStats>
        <use>standard</use>
        <args>
          <module>MyCompany_CustomerStats</module>
          <frontName>userstats</frontName>
        </args>
      </CustomerStats>
    </routers>
        <layout>
        <updates>
          <CustomerStats>
                  <file>customerstats.xml</file>
                </CustomerStats>
          </updates>
        </layout>
  </frontend>
</config>

更新2-2012/07/28
我承認,從MVC的角度來看,我進行的實驗對我完全沒有意義。 我更改了布局並將塊類型指定為customerstats / userstatsdashboard 然后,我修改了Block類MyCompany_CustomerStats_Block_UserStatsDashboard ,如下所示:

public function _construct() {
    echo "Block rendered.";
}

令人驚訝的是,現在新布局中出現了一些東西,正好在我期望的位置! 但這意味着根本沒有調用Controller,這又意味着沒有數據可用於這些塊。
這真的讓我感到困惑,我從未見過Views可以自己管理的MVC模型……那么擴展控制器的目的是什么?

您的塊類組是CustomerStats ,但是您沒有在布局中使用它。 Block類群組只是在獨特的文本節點, global/blocks ,絕不需要“匹配”的任何文件夾結構(僅供參考)。 該字符串僅在與對createBlock()的任何調用的第一部分(斜杠之前)相匹配createBlock() ,這意味着您的布局XML應該指定例如type="CustomerStats/whatever"

在上面的例子會產生類名稱將MyCompany_CustomerStats_Block_Whatever基於對你的類前綴MyCompany_CustomerStats_Block

在不考慮例外的情況下,存在兩種類型的塊。 一個core/text_list和所有其他類型。 例如,當引用core/text_listcontent塊將自動呈現所有添加的塊。

當引用不同於core/text_list ,對於customer/account_dashboard您需要自己從模板中獲取內容。 就像您已經建議自己一樣,可以通過getChildHtml()調用完成此操作。

盡管到目前為止我無法使用您提供的代碼進行驗證,但可能是錯誤的是,您的模塊中找不到您自己的customerstats/index塊類型,或者沒有擴展Mage_Core_Block_Template 或者您要將不存在的類組傳遞到createBlock工廠方法中。

因此,在您的模塊內應該有以下內容:

    <blocks>
        <customerstats>
            <class><your_namespace>_Customerstats_Block</class>
        </customerstats>
    </blocks>

但是我認為您那里可能有一個名為customerdata的句柄,而不是customerstats 由於您從控制器調用的塊的類型為customerdata/customerstatsdashboard ,因此該塊有效。 雖然在您的XML中使用customerstats那個。 如果是這種情況,您可能想要嘗試將XML調整為:

<block type="customerdata/index" name="customerstats_dashboard" as="customerstats.dashboard" template="customerdata/customerstatsdashboard.phtml" />

此外,使用模板時,您的塊應該看起來像。 因此,總是擴展Mage_Core_Block_Template。 否則getChildHtml將不可用。 <your_namespace>_Customerstats_Block_Index extends Mage_Core_Block_Template

希望這可以幫助您完成任務,否則請在嘗試后提供反饋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM