Question

I want to show message on some condition in a template file(custom module template file).I am having following code.

<?php
 if(count($collection)): ?>             
        <?php foreach($collection as $coll): ?>         
                   some calculations                   
        <?php endforeach; ?>
<?php else: ?>
        <?php $message = $this->__('There is no data available'); ?>
        <?php echo Mage::getSingleton('core/session')->addNotice($message);?>
<?php endif;?>

But this is not working properly. The message is displayed on other pages not on the same page.

Was it helpful?

Solution 2

Muk,According to your code

  Mage::getSingleton('core/session')->addNotice($message);

add an notice to magento.This is code set notice to session and which is reflecting on page refresh according php session ,a session variable set value is reflecting after page refresh.

If you already added this notice on other page before came to your file then ,you need to add core/session to custom module template file controllers file. Code is $this->_initLayoutMessages('core/session');

In controller you need below code in controller.

/* load the layout from xml */
        $this->loadLayout();

        $this->_initLayoutMessages('core/session');

        /* rendering layout */
        $this->renderLayout();

Read more at inchoo blog

OTHER TIPS

If you really need to implement that right in the template, you may use the code below:

<?php echo $this->getLayout()->createBlock('core/messages')->addNotice('My Message')->toHtml(); ?>

But the solution described by Amit Bera sounds like a better way to resolve it.

I found that $this->loadLayout() is what reads and clears messages from the session, therefore if you add messages before calling $this->loadLayout() then those messages should be displayed on the current page.

Example:

public function chooseFileAction() {

    // a quick and dirty way to find the larger out of post_max_size and upload_max_filesize
    $post_max_size = ini_get('post_max_size'); $post_max_size_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $post_max_size)));
    $upload_max_filesize = ini_get('upload_max_filesize'); $upload_max_filesize_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $upload_max_filesize)));
    $maxsize = $post_max_size_int < $upload_max_filesize_int ? $post_max_size : $upload_max_filesize;

    // display max file size to user in a message box
    $msg = 'Max file size: ' . $maxsize;
    Mage::getSingleton('core/session')->addNotice($msg);

    $this->loadLayout();

    $this->_title($this->__('Catalog'))->_title($this->__('File Upload'));
    $this->_setActiveMenu('catalog/customfileupload');

    $block = $this->getLayout()->createBlock('customfileupload/adminhtml_customfileupload_choosefile');
    $this->_addContent($block);

    $this->renderLayout();

}

Caveat: this is tested in Magento 1.9, not 1.7.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top