实际上,当缓存被清除并且我访问一个页面时,块和相应的模板加载完美。然后,当我重新访问此页面时,块根本没有加载。我在这个打孔过程中错过了什么吗?

模型容器:

class Myname_Page_Model_Container_Ajaxcart extends Enterprise_PageCache_Model_Container_Abstract
{
    protected function _renderBlock()
    {
        $blockClass = $this->_placeholder->getAttribute('block');
        $template = $this->_placeholder->getAttribute('template');

        $block = new $blockClass;
        $block->setTemplate($template);
        return $block->toHtml();
    }
}

缓存。xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <placeholders>
        <ajax_cart>
            <block>page/html_ajaxcart</block>
            <name>ajaxCart</name>
            <placeholder>AJAX_CART</placeholder>
            <container>Myname_Page_Model_Container_Ajaxcart</container>
            <cache_lifetime></cache_lifetime>
        </ajax_cart>
    </placeholders>
</config>

布局。xml:

<block type="page/html_ajaxcart" name="ajaxCart" as="ajaxCart" template="page/html/ajaxCart.phtml"/>

模板。phtml,phtml:

<?php echo $this->getChildHtml('ajaxCart');?>

配置文件:

<?xml version="1.0"?>
<config>
    <modules>
        <Myname_Page>
            <version>0.1.0</version>
        </Myname_Page>
    </modules>
    <frontend>
        <routers>
            <page>
                <use>standard</use>
                <args>
                    <module>Myname_Page</module>
                    <frontName>page</frontName>
                </args>
            </page>
        </routers>
    </frontend>
    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_ajaxcart>Myname_Page_Block_Html_Ajaxcart</html_ajaxcart>
                </rewrite>
            </page>
            <myname_page>
                <class>Myname_Page_Block</class>
            </myname_page>
        </blocks>
    </global>
</config>

块文件:

<?php
class Myname_Page_Block_Html_Ajaxcart extends Mage_Core_Block_Template
{
    // sort quote by item date of addition
    public function sortByUpdatedAt($quote)
    {
        // initialize item collection
        $items = array();
        $collection = $quote->getAllVisibleItems();

        // get updated at date for each item
        foreach ($collection as $item) {
            if (!$item->isDeleted()) {
                array_push($items, $item->getData('updated_at'));
            }
        }


        // sort and keep key association
        asort($items);

        // reverse order and keep key association
        $k = array_reverse(array_keys($items));
        $reversed = array_reverse(array_values($items));
        $items = array_combine($k, $reversed);

        // collection is sorted
        $collection = $this->finishSort($items, $collection);
        return $collection;
    }

    // sort collection by established ordering
    protected function finishSort($items, $collection)
    {
        $sortedCollection = array();

        // collection will reflect sorted item array
        foreach ($items as $key => $value) {
            // get new order by established key value order
            array_push($sortedCollection, $collection[$key]);
        }
        return $sortedCollection;
    }

    public function getProductPrice($product)
    {
        // return formatted special price
        if ($product->getSpecialPrice() != null && Mage::app()->getLocale()->isStoreDateInInterval(Mage::app()->getStore(), $product->getSpecialFromdate(), $product->getSpecialToDate())) {
            $price = $product->getSpecialPrice();
        } else {
            $price = $product->getPrice();
        }
        // return formatted price
        return Mage::helper('core')->formatPrice($price, true);
    }
}
有帮助吗?

解决方案

我相信你错过了一个实现 _getCacheId & _saveCache

如果没有提供但具有匿名命名约定,则已经定义了cache_id。为块定义一个ID,并在您的 _getCacheId

添加一个 etc/cache.xml 文件与 <config> 根到你的模块。(见 Enterprise/PageCache/etc/cache.xml).选择一个独特的 [占位符] 姓名。

placeholders/[placeholder]/block 节点值必须匹配 class-id 自定义动态块,例如mymodule/自定义

placeholders/[placeholder]/container 节点值是要 动态生成内容并处理块级缓存

placeholders/[placeholder]/placeholder 节点值是唯一的 字符串来标记缓存页面中的动态部分

placeholders/[placeholder]/cache_lifetime 使用被忽略,但不是 再也没有了。对于较旧的实例,在 货柜的 _saveCache() 方法代替。

实现容器类并扩展 Enterprise_PageCache_Model_Container_Abstract.使用方法 _renderBlock() 到 返回动态内容。

实施 _getCacheId() 返回唯一字符串的方法。使用方法 cookie值而不是模型id(成本较低)。

实施;实施 _saveCache() 简单地返回或传递缓存生存期 值给父方法。瞧,动态块完成。

最后一个音符:你 不要 有完整的Magento应用程序在您的处置 何时 _renderBlock() 被调用。尽可能保守。

其他提示

我想你错过了 <template>cache.xml 档案。但我不确定这一点。

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <placeholders>
        <ajax_cart>
            <block>page/html_ajaxcart</block>
            <name>ajaxCart</name>
            <placeholder>AJAX_CART</placeholder>
            <template>page/html/ajaxCart.phtml</template> <!-- Your template file -->
            <container>Myname_Page_Model_Container_Ajaxcart</container>
            <cache_lifetime>false</cache_lifetime>
        </ajax_cart>
    </placeholders>
</config>

尝试上面的代码它可能会工作。

许可以下: CC-BY-SA归因
scroll top