在产品页面选项卡中有条件地显示块


Conditionally Displaying Blocks In Product Page Tabs

我正在更改Magento 1.5网站的前端布局,并试图将我的产品评论从一个单独的页面移动到目录中产品页面下部的"更多信息"选项卡之一。因此,我想让这个选项卡的内容成为有条件的——我希望选项卡在现有评论存在时显示它们,但在产品还没有评论时显示评论创建表单。

以下是我到目前为止所尝试的,我正在尝试组合的部分。

/app/design/frontend/our site/default/review.xml:

<catalog_product_view translate="label">
    <label>Catalog Product View</label>
    <reference name="product.info.tabs">
        <action method="addTab" translate="title" module="review">
            <alias>product.reviews</alias>
            <title>Reviews</title>
            <block>review/form</block>
            <template>review/form.phtml</template>
        </action>
    </reference>
</catalog_product_view>                                            

此XML导致"留下您的评论"表单显示在产品页面上名为"评论"的选项卡下。

我在/app/code/core/Mage/Catalog/Block/Product/View/Tabs.php中找到了addTab函数,我可以大致了解它是如何工作的。然而,我认为我还没有看到一种方法可以使用XML来限制页面上应该显示什么。

我注意到summary.phtml文件做了一些我想要的事情:它有:

if ($this->getReviewsCount()): ?>
    <div class="ratings">
        <?php if ($this->getRatingSummary()):?>
             <div class="rating-box">
                <div class="rating" style="width:<?php echo $this->getRatingSummary() ?>%"></div>
             </div>
    <?php endif;?>
         <p class="rating-links">
             <a href="<?php echo $this->getReviewsUrl() ?>"><?php echo $this->__('%d Review(s)', $this->getReviewsCount()) ?></a>
             <span class="separator">|</span>
             <a href="<?php echo $this->getReviewsUrl() ?>#review-form">
                 <?php echo $this->__('Add Your Review') ?></a>
         </p>
    </div>
<?php elseif ($this->getDisplayIfEmpty()): ?>
    <p class="no-rating">
        <a href="<?php echo $this->getReviewsUrl() ?>#review-form">
            <?php echo $this->__('Be the first to review this product') ?>
        </a>
    </p>
<?php endif; ?>

看起来它有条件函数"如果有评论就这么做,如果没有评论就另当别论"。我如何在渲染链的早期调用它来决定渲染到页面中的块的上下文?这是需要在XML部分的phtml部分中完成的任务吗?

您可能需要创建自己的phtml文件,将摘要文件作为起点进行复制,但在其他页面中,而不是呈现链接,而是呈现其他页面上包含的Block。然后,您可以调整该块的xml文件,通过替换template属性来告诉它您使用了新的模板文件。您可以使用xml布局将评审表单块添加为子块,然后使用echo $this->getChildHtml('as_field_of_block');,或者将其硬编码到模板中。前一种选择可能更灵活,但需要对xml进行一些额外的调整。