getChildHtml('p...">

How to make a condition in purple?

there is a code like this:

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

      

I want to add an if condition before <div class="test">

. How should I do it? Thank. when i add the following code it shows me an error. why?

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


    <?php endif;?>

      

+3


source to share


2 answers


From PHP docs:

Attention

isset () only works on variables, since passing anything else will result in a parsing error. To check if constants are set, use the defined () function.



You are passing the return value to a function that is not valid. You need to do something like

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

      

+3


source


If you look at the magenta code, the getChildHtml function will return a string.

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

      



If you look a little more, it seems that the function returns an empty string if there is nothing to render. So I would just render the returned html without any conditions. If you really need to know if there is something, I would do: if ($ this-> getChildHtml ($ name)! = '')

+2


source







All Articles