Magento块附带空输出 [英] Magento Block Coming with empty output

查看:92
本文介绍了Magento块附带空输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的新闻模块.提供前端和后端的功能.管理员功能正常运行.

I have created simple News Module. which serves functionality from frontend as well as backend. Admin functionality works fine.

但是在前端,

  1. 我有两页. i)新闻列表页面 ii)新闻详细信息页面
  1. I have two pages. i) News List page ii) News Detail Page

此处新闻列表页面运行正常.但是新闻详细信息页面的结果是空的.

Here News list page is working perfect. But the News detail page is coming with empty result.

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

推荐答案

为什么在视图中有setPage?

Why do you have setPage in your view?

    $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); 
    */

这篇关于Magento块附带空输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆