Magento Block来自空输出

时间:2014-03-14 10:17:53

标签: php magento templates block

我创建了简单的新闻模块。它提供来自前端和后端的功能。管理功能正常。

但是在前端,

  1. 我有两页。 i)新闻列表页面 ii)新闻详情页面
  2. 这里的新闻列表页面工作正常。但是新闻详情页面的结果是空洞的。

    我可以使用模型从数据库表中获取结果,但无法将其呈现给模板文件。

    配置文件

    <?xml version="1.0"?>
    <!--
    /**
     * Module configuration
     *
     * @author Magento
     */
    -->
    <config>
        <modules>
            <Magentostudy_News>
                <version>1.0.0.0.1</version>
            </Magentostudy_News>
        </modules>
        <global>
            <models>
                <magentostudy_news>
                    <class>Magentostudy_News_Model</class>
                    <resourceModel>news_resource</resourceModel>
                </magentostudy_news>
                <news_resource>
                    <class>Magentostudy_News_Model_Resource</class>
                    <entities>
                        <news>
                            <table>magentostudy_news</table>
                        </news>
                    </entities>
                </news_resource>
            </models>
            <helpers>
                <magentostudy_news>
                    <class>Magentostudy_News_Helper</class>
                </magentostudy_news>
            </helpers>
            <blocks>
                <magentostudy_news>
                    <class>Magentostudy_News_Block</class>
                </magentostudy_news>
            </blocks>
            <resources>
                <magentostudy_news_setup>
                    <setup>
                        <module>Magentostudy_News</module>
                        <class>Mage_Core_Model_Resource_Setup</class>
                    </setup>
                </magentostudy_news_setup>
            </resources>
          <!--  <events>
                <before_news_item_display>
                    <observers>
                        <magentostudy_news>
                            <class>magentostudy_news/observer</class>
                            <method>beforeNewsDisplayed</method>
                        </magentostudy_news>
                    </observers>
                </before_news_item_display>
            </events>-->
        </global>
        <frontend>
            <routers>
                <magentostudy_news>
                    <use>standard</use>
                    <args>
                        <module>Magentostudy_News</module>
                        <frontName>news</frontName>
                    </args>
                </magentostudy_news>
            </routers>
            <layout>
                <updates>
                    <magentostudy_news>
                        <file>magentostudy_news.xml</file>
                    </magentostudy_news>
                </updates>
            </layout>
        </frontend>
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <Magentostudy_News before="Mage_Adminhtml">Magentostudy_News_Adminhtml</Magentostudy_News>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
        <adminhtml>
            <layout>
                <updates>
                    <magentostudy_news>
                        <file>magentostudy_news.xml</file>
                    </magentostudy_news>
                </updates>
            </layout>
        </adminhtml>
        <default>
            <news>
                <view>
                    <enabled>1</enabled>
                    <items_per_page>20</items_per_page>
                    <days_difference>3</days_difference>
                </view>
            </news>
        </default>
    </config>
    

    索引控制器

    <?php
     /**
     * News frontend controller
     *
     * @author Magento
     */
    class Magentostudy_News_IndexController extends Mage_Core_Controller_Front_Action
    {
        /**
         * Pre dispatch action that allows to redirect to no route page in case of disabled extension through admin panel
         */
        public function preDispatch()
        {
            parent::preDispatch();
    
            if (!Mage::helper('magentostudy_news')->isEnabled()) {
                $this->setFlag('', 'no-dispatch', true);
                $this->_redirect('noRoute');
            }
        }
    
        /**
         * Index action
         */
        public function indexAction()
        {
            $this->loadLayout();
    
            $listBlock = $this->getLayout()->getBlock('news.list');
    
            if ($listBlock) {
                $currentPage = abs(intval($this->getRequest()->getParam('p')));
                if ($currentPage < 1) {
                    $currentPage = 1;
                }
                $listBlock->setCurrentPage($currentPage);
            }
    
            $this->renderLayout();
        }
    
        /**
         * News view action
         */
        public function viewAction()
        {
            $newsId = $this->getRequest()->getParam('id');
            if (!$newsId) {
                return $this->_forward('noRoute');
            }
    
            /** @var $model Magentostudy_News_Model_News */
            $model = Mage::getModel('magentostudy_news/news');
            $model->load($newsId);
    
            if (!$model->getId()) {
                return $this->_forward('noRoute');
            }
    
            Mage::register('news_item', $model);
    
            Mage::dispatchEvent('before_news_item_display', array('news_item' => $model));
    
            $this->loadLayout();
            $itemBlock = $this->getLayout()->getBlock('news.item');
            if ($itemBlock) {
                $listBlock = $this->getLayout()->getBlock('news.list');
                if ($listBlock) {
                    $page = (int)$listBlock->getCurrentPage() ? (int)$listBlock->getCurrentPage() : 1;
                } else {
                    $page = 1;
                }
                $itemBlock->setPage($page);
            }
            $this->renderLayout();
        }
    }
    

    阻止文件:Item.php

    <?php
    /**
     * News Item block
     *
     * @author Magento
     */
    class Magentostudy_News_Block_Item extends Mage_Core_Block_Template
    {
    
        /**
         * Current news item instance
         *
         * @var Magentostudy_News_Model_News
         */
        protected $_item;
    
        /**
         * Return parameters for back url
         *
         * @param array $additionalParams
         * @return array
         */
        protected function _getBackUrlQueryParams($additionalParams = array())
        {
            return array_merge(array('p' => $this->getPage()), $additionalParams);
        }
    
        /**
         * Return URL to the news list page
         *
         * @return string
         */
        public function getBackUrl()
        {
            return $this->getUrl('*/', array('_query' => $this->_getBackUrlQueryParams()));
        }
    
        /**
         * Return URL for resized News Item image
         *
         * @param Magentostudy_News_Model_News $item
         * @param integer $width
         * @return string|false
         */
        public function getImageUrl($item, $width)
        {
            return Mage::helper('magentostudy_news/image')->resize($item, $width);
        }
    }
    

    布局文件:Magentostudy_news.xml

    <?xml version="1.0"?>
    <!--
    /**
     * Magento frontend layout
     *
     * @author Magento
     */
    -->
    
        <layout version="0.1.0">
    
            <default>
                 <reference name="footer_links">
                     <action method="addLink" translate="label title" module="magentostudy_news" ifconfig="news/view/enabled">
                         <label>News</label>
                         <url>news</url>
                         <title>News</title>
                         <prepare>true</prepare>
                     </action>
                 </reference>
            </default>
    
            <magentostudy_news_index_index translate="label">
                <label>News Page</label>
                <reference name="root">
                    <action method="setTemplate">
                        <template>page/2columns-right.phtml</template>
                    </action>
                    <action method="setHeaderTitle" translate="title" module="magentostudy_news">
                        <title>Site News</title>
                    </action>
                </reference>
    
                <reference name="content">
                    <block type="magentostudy_news/list" name="news.list" template="magentostudy/news/list.phtml">
                        <block type="page/html_pager" name="news.list.pager" as="news_list_pager" />
                    </block>
                </reference>
            </magentostudy_news_index_index>
    
            <magentostudy_news_index_view translate="label">
                <label>News Item Page</label>
                <reference name="root">
                    <action method="setTemplate">
                        <template>page/2columns-right.phtml</template>
                    </action>
                </reference>
                <reference name="content">
                    <block type="magentostudy_news/news" name="news.item" template="magentostudy/news/item.phtml" />
                </reference>
            </magentostudy_news_index_view>
    
        </layout>
    

    新闻/ item.phtml

    <?php
    /**
     * News template for separate item
     *
     * @author Magento
     */
    /**
     * @var $this Magentostudy_News_Block_Item
     * @see Magentostudy_News_Block_Item
     */
    ?>
    <?php $_newsItem = $this->helper('magentostudy_news')->getNewsItemInstance();
    ?>
    <div id="news_item_messages"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
    <div>
        <h1><?php echo $this->escapeHtml($_newsItem->getTitle()) ?></h1>
        <div>
            <?php echo $this->formatDate($_newsItem->getTimePublished(),
    Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM) ?> |
            <?php echo $this->escapeHtml($_newsItem->getAuthor()) ?>
        </div>
    </div>
    <div>
        <?php if ($imageUrl = $this->getImageUrl($_newsItem, 400)): ?>
           <p><img src="<?php echo $imageUrl ?>" alt="<?php echo $this->escapeHtml($_newsItem->getTitle()) ?>" /></p>
        <?php endif; ?>
    <div><?php echo $_newsItem->getContent() ?></div>
        <div>
            <a href="<?php echo $this->getBackUrl() ?>">
                 <?php echo Mage::helper('magentostudy_news')->__('Return to list') ?>
            </a>
    
        </div>
    </div>
    

1 个答案:

答案 0 :(得分:0)

为什么你的视图中有setPage?

    $this->loadLayout();
    $itemBlock = $this->getLayout()->getBlock('news.item');
    if ($itemBlock) {
        $listBlock = $this->getLayout()->getBlock('news.list');
        if ($listBlock) {
            $page = (int)$listBlock->getCurrentPage() ? (int)$listBlock->getCurrentPage() : 1;
        } else {
            $page = 1;
        }
        $itemBlock->setPage($page);

你试过删除吗?

    $this->loadLayout();
    $itemBlock = $this->getLayout()->getBlock('news.item');
    /* if ($itemBlock) {
        $listBlock = $this->getLayout()->getBlock('news.list');
        if ($listBlock) {
            $page = (int)$listBlock->getCurrentPage() ? (int)$listBlock->getCurrentPage() : 1;
        } else {
            $page = 1;
        }
        $itemBlock->setPage($page); 
    */