2012-03-19 60 views
3

有一些代码,因为这:如何做magento的条件?

<div class="test"> 
     <div class="upsell-tags"> 
    <?php echo $this->getChildHtml('product_additional_data') ?> 
    <?php echo $this->getChildHtml('upsell_products') ?> 
     </div> 
    </div> 

我想<div class="test">前添加一个if条件。我该怎么做?谢谢。 当我添加下面的代码,它显示我一个错误。为什么?

<?php if(isset($this->getChildHtml('upsell_products'))):?>..... 


    <?php endif;?> 

回答

3

从PHP文档:

警告

isset()函数只适用于变量,否则传递任何将导致解析错误。为了检查是否设置了常量,使用defined()函数。

您传递的函数的返回值不是有效的用法。你需要做类似

$upsell = $this->getChildHtml('upsell_products'); 
if($upsell) { 
    // ... 
} 
2

如果你看看magento代码,函数getChildHtml返回一个字符串。

/** 
* Retrieve child block HTML 
* 
* @param string $name 
* @param boolean $useCache 
* @param boolean $sorted 
* @return string 
*/ 
public function getChildHtml($name = '', $useCache = true, $sorted = false) 

如果你看起来更多一点,看起来该函数返回一个空字符串,如果没有什么要呈现。所以我只是显示返回的HTML没有任何条件。如果你真的需要知道是否有什么东西,我会这样做:if($ this-> getChildHtml($ name)!='')