홀 펀치 블록이 전체 페이지 캐시 페이지 내에 로드되지 않습니다.
-
13-12-2019 - |
문제
처음에 캐시가 지워지고 페이지를 방문하면 블록과 해당 템플릿이 완벽하게 로드됩니다.그런 다음 이 페이지를 다시 방문하면 블록이 전혀 로드되지 않습니다.이 구멍을 뚫는 과정에서 제가 뭔가를 놓치고 있는 걸까요?
모델 컨테이너:
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"/>
template.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
아무것도 제공되지 않았지만 익명 명명 규칙을 따르는 경우에는 캐시 ID가 이미 정의되어 있습니다.블록의 ID를 정의하고 이를 _getCacheId
을 추가하다
etc/cache.xml
파일을<config>
모듈의 루트입니다.(보다Enterprise/PageCache/etc/cache.xml
).독특한 것을 선택하세요 [자리 표시자]이름.그만큼
placeholders/[placeholder]/block
노드 값은 다음과 일치해야 합니다.class-id
사용자 정의 동적 블록의 예:내 모듈/사용자 정의그만큼
placeholders/[placeholder]/container
node 값은 콘텐츠를 동적으로 생성하고 블록 수준 캐싱을 처리합니다.그만큼
placeholders/[placeholder]/placeholder
노드 값은 캐시 된 페이지의 동적 부품을 표시하는 고유 한 문자열입니다.
placeholders/[placeholder]/cache_lifetime
use를 사용하여 무시할 수 있지만 무시할 수는 없습니다. 더 이상.이전 인스턴스의 경우 콘테이너의_saveCache()
대신 방법.컨테이너 클래스 구현 및 확장
Enterprise_PageCache_Model_Container_Abstract
.사용_renderBlock()
받는 사람 동적 콘텐츠를 반환합니다.구현
_getCacheId()
고유한 문자열을 반환하는 메서드입니다.모델 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>
.
위의 코드를 사용해보십시오.