如何从容器中检索ProductId参数?
答案:你不要从容器中检索一个参数,但要从你的块中检索。 我已经在堆栈上阅读了答案,但没有一个告诉我如何从容器中检索参数.. 编辑:感谢答案,我重新回答了我的问题!下面的工作代码。View.php:/ app / code / local / Stackoverflow / Testcase / Block
class Stackoverflow_Testcase_Block_View extends Mage_Core_Block_Template
{
public function getParameters() {
if (Mage::registry('current_product')) {
$product_id = Mage::registry('current_product')->getId();
} else {
$product_id = $this->getProductId();
}
//Make sure we are not cached
echo "time:".time()."<br/>";
//This variable will be visible after the second load because it must be cached first!
//NOTE that you are not able to save variables with an underscore ;-)
echo "test_var:".$this->getTestVar()."<br/>";
return "product_id: ".$product_id;
}
public function getCacheKeyInfo()
{
$info = parent::getCacheKeyInfo();
if (Mage::registry('current_product'))
{
$info['product_id'] = Mage::registry('current_product')->getId();
}
$info['test_var'] = "first second third";
return $info;
}
}
Testcase.php:/ app / code / local / Stackoverflow / Testcase / Model / Container
class Stackoverflow_Testcase_Model_Container_Testcase extends Enterprise_PageCache_Model_Container_Abstract {
protected function _getIdentifier()
{
return $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '');
}
protected function _getCacheId()
{
return 'TESTCASE' . md5($this->_placeholder->getAttribute('cache_id') . ',' . $this->_placeholder->getAttribute('product_id')) . '_' . $this->_getIdentifier();
}
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template)
->setProductId($this->_placeholder->getAttribute('product_id'))
->setTestVar($this->_placeholder->getAttribute('test_var'));
return $block->toHtml();
}
protected function _saveCache($data, $id, $tags = array(), $lifetime = null) { return false; }
}
testcase.phtml:app / design / frontend / xxx / default / template / stackoverflow
$block_html = $this->getParameters();
echo $block_html;
?>
Catalog.xml:app / design / frontend / ll / default / layout
`
<label>Catalog Product View (Simple)</label>
<reference name="product.info">
<block type="catalog/product_view_type_simple" name="product.info.simple" as="product_type_data" template="catalog/product/view/type/default.phtml">
<block type="core/text_list" name="product.info.simple.extra" as="product_type_data_extra" translate="label">
<label>Product Extra Info</label>
</block>
<block type="testcase/view" name="testcase" as="testcase" template="stackoverflow/testcase.phtml"/>
</block>
</reference>
</PRODUCT_TYPE_simple>
`
default.phtml:app / design / frontend / xxx / default / template / catalog / product / view /
<?php echo $this->getChildHtml('testcase') ?>
下面的代码可能不相关但...... config.xml:/ app / code / local / Stackoverflow / Testcase / etc
<?xml version="1.0"?>
<config>
<modules>
<Stackoverflow_Testcase>
<version>0.1.0</version>
</Stackoverflow_Testcase>
</modules>
<global>
<blocks>
<testcase>
<class>Stackoverflow_Testcase_Block</class>
</testcase>
</blocks>
</global>
</config>
cache.xml:/ app / code / local / Stackoverflow / Testcase / etc
<?xml version="1.0" encoding="UTF-8"?>
<config>
<placeholders>
<stackoverflow_testcase>
<block>testcase/view</block>
<name>testcase</name>
<placeholder>STACKOVERFLOW_TESTCASE</placeholder>
<container>Stackoverflow_Testcase_Model_Container_Testcase</container>
<cache_lifetime>1</cache_lifetime>
</stackoverflow_testcase>
</placeholders>
</config>
Stackoverflow_Testcase.xml:/ app / etc / modules
<?xml version="1.0"?>
<config>
<modules>
<Stackoverflow_Testcase>
<active>true</active>
<codePool>local</codePool>
</Stackoverflow_Testcase>
</modules>
</config>
谢谢,
马亭
答案 0 :(得分:1)
以Enterprise_PageCache_Model_Container_Sidebar_Cart
容器为例:
文件: 应用程序/代码/核心/企业/页缓存/型号/容器/边栏/ Cart.php
$renders = $this->_placeholder->getAttribute('item_renders');
要访问容器中的变量,您需要将其添加到Block类中的cachekeyinfo
:
文件:app / code / core / Mage / Checkout / Block / Cart / Sidebar.php
public function getCacheKeyInfo()
{
$cacheKeyInfo = parent::getCacheKeyInfo();
$cacheKeyInfo['item_renders'] = $this->_serializeRenders();
return $cacheKeyInfo;
}
编辑
在你的容器中试试这个:
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
$block = new $blockClass;
$block->setTemplate($template)
->setProductId($this->_placeholder->getAttribute('product_id'););
return $block->toHtml();
}
并在您的块中尝试此操作:
public function getCacheKeyInfo()
{
$cacheKeyInfo = parent::getCacheKeyInfo();
$cacheKeyInfo['product_id'] = $this->getRequest()->getParam('ProductId');
return $cacheKeyInfo;
}