2014-04-15 9 views
1

템플릿 파일 (사용자 정의 모듈 템플릿 파일)의 일부 조건에 대한 메시지를 표시하려고합니다. 다음 코드가 있습니다.템플릿 파일에 오류 또는 성공 메시지를 표시하는 방법은 무엇입니까?

<?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;?> 

하지만 제대로 작동하지 않습니다. 같은 페이지가 아닌 다른 페이지에 메시지가 표시됩니다. magento.This는 code set notice to session이며, PHP는 세션, 세션 변수 설정 값이 페이지 새로 고침 후 반영에 따라 페이지 새로 고침에 반영되는

답변

4

묵, 당신의 코드

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

에 따르면 통지를 추가 할 수 있습니다.

파일에 오기 전에 이미 다른 페이지에이 알림을 추가 한 경우 y ou need to add core/session to custom module template file controllers file입니다. 코드는 $this->_initLayoutMessages('core/session');

컨트롤러에는 컨트롤러에 다음 코드가 필요합니다. 미트 Bera 설명

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

그러나 솔루션 : 당신이 정말로 템플릿에 그 권리를 구현해야하는 경우

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

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

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

아래의 코드를 사용할 수 있습니다, inchoo blog

5

에서 더 읽기 그것을 해결하는 더 좋은 방법 같아.

0

$this->loadLayout()은 세션에서 메시지를 읽고 지우는 메시지이므로 메시지를 추가하기 전에 $this->loadLayout()을 호출하면 해당 메시지가 현재 페이지에 표시되어야합니다.

예 :

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(); 

} 

경고 :이 아닌 젠토 1.7 1.9 시험한다.

관련 문제