穴のパンチブロックは全ページキャッシュページ内にロードされません
-
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();
}
}
.
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>
<container>Myname_Page_Model_Container_Ajaxcart</container>
<cache_lifetime></cache_lifetime>
</ajax_cart>
</placeholders>
</config>
.
layout.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
cache_idは、何も提供されていないが匿名の命名規則の場合は既に定義されています。ブロックのIDを定義し、それを_getCacheId
etc/cache.xml
ファイルをモジュールに<config>
ルートを追加します。 (見るEnterprise/PageCache/etc/cache.xml
)。一意の [プレースホルダー] を選択してください。 名前。
placeholders/[placeholder]/block
ノード値と一致する必要があります。 カスタムダイナミックブロックのclass-id
。 MyModule / Customを処理します。
placeholders/[placeholder]/container
ノード値はクラスです。 コンテンツを動的に生成し、ブロックレベルキャッシュ
placeholders/[placeholder]/placeholder
ノード値は一意です キャッシュされたページの動的部分をマークする文字列
placeholders/[placeholder]/cache_lifetime
の使用は無視されるが、そうではない もう。古いインスタンスの場合、ブロックキャッシュの有効期間を指定します。 代わりにコンテナの_saveCache()
メソッドコンテナクラスを実装して拡張します
Enterprise_PageCache_Model_Container_Abstract
。_renderBlock()
toを使用してください 動的コンテンツを返します。固有の文字列を返すための
_getCacheId()
メソッドを実装します。使用する モデルIDの代わりにCookie値(低コスト)単純に戻る、またはキャッシュの有効期間を通過するように
_saveCache()
を実装する 親メソッドへの値。 Voila、動的ブロックが完了しました。1つの最後のノート:あなたではありませんあなたの処分でフルマゼントアプリを持っています
_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>
.
上記のコードを試してみてください。