如何从容器中检索 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>
谢谢,
马汀