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

问题描述:

我正在更改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在 '留下您的评论' 表单结果被一个叫选项卡下显示 “点评”在产品页面上。

我跟踪了'addTab函数/app/code/core/Mage/Catalog/Block/Product/View/Tabs.php,我可以看看它是如何工作的。不过,我认为我没有看到使用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文件,将该摘要文件复制为起点,但在其他位置,而不是渲染链接,以呈现包含在该其他页面中的块。然后,您可以调整该块的xml文件,通过替换模板属性来告诉它使用新的模板文件。您可以使用xml布局将评论表单块添加为小孩,然后使用echo $this->getChildHtml('as_field_of_block');,或者将其硬编码到模板中。前者可能更灵活,但需要对xml进行一些额外的调整。

+0

所以我听说你说没有办法在配置XML中表达一个条件,如果我想快速做到这一点(而不是正确的),我应该使用一个普通的ol'if-switch在' .phtml文件的PHP - 是吗? – 2012-01-27 15:21:24

+0

事实证明,有一个现有的.phtml文件做了我想做的事情,所以我使用了它。我用你的建议意识到phtml文件做了我想要的,所以我接受了这个答案。谢谢。 :) – 2012-01-30 18:32:23