duanpan3166 2017-08-23 13:14
浏览 49
已采纳

SilverStripe 3.6.1 - if / else_if语句未在模板中设置正确的字段

I'm trying to use an if/else_if statement in my homepage template to check which one of 2 fields have a value. Depending on which field has a value, a link is setup using the value of said variable.

However, as it stands, the setup I have does not work. I have an item with the ImageExternalLink field filled in but the statement seems to still think it's an internal link.

See below--this statement always sets the link as an Internal Link. It's almost like the else_if never is executed, although, I'm not sure why? I feel as if I am following the setup correctly based on the documentation.

div class="halfColumn">
   <% if $ImageLinkInternalURL.Link %>
     <a href="$ImageLinkInternalURL.Link">
         <p>Internal Link</p>
     </a>
    <% else_if $ImageExternalLink %>
        <a href="$ImageExternalLink">
            <p>External Link</p>
        </a>
    <% else %>
        <img class="dropShadow" src="$Image.URL" style="max-height: 220px;">
    <% end_if %>
</div>

For reference, here is the HomePageCallout.php file for reference, which is where the ImageLinkInternalURL and ImageExternalLink fields are created:

<?php

class HomePageCallout extends DataObject {

    private static $db = array(
        'SortOrder'     => 'Int',
        'ButtonText'    => 'varchar',
        'Header'        => 'varchar',
        'ImageExternalLink'  => 'varchar',
        'Description'   => 'HTMLText'
    );

    static $default_sort = "SortOrder ASC";

    // One-to-one relationship with gallery page
    private static $has_one = array(
        'Image' => 'Image',
        'Page' => 'Page',
        'ImageLinkInternalURL' => 'SiteTree',
    );

    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","PageID");
        $fields->removeFieldFromTab("Root.Main","SortOrder");

        $fields->addFieldToTab("Root.Main", new NumericField("SortOrder"));
        $fields->addFieldToTab("Root.Main", new TextField("ImageExternalLink"), "SortOrder");

        return $fields;
    }

    // Tell the datagrid what fields to show in the table
    public static $summary_fields = array(
        'ID' => 'ID',
        'Header' => 'Header',
        'Thumbnail' => 'Thumbnail',
    );

    // this function creates the thumnail for the summary fields to use
    public function getThumbnail() {
        return $this->Image()->CMSThumbnail();
    }

    public function canEdit($member = NULL) {
        return true;
    }

    public function canDelete($member = NULL) {
        return true;
    }

    public function canCreate($member = NULL){
        return true;
    }

    public function canPublish(){
        return true;
    }

    public function canView($member = NULL){
        return true;
    }
}
  • 写回答

1条回答 默认 最新

  • dongpin3794 2017-08-24 07:21
    关注

    $ImageLinkInternalURL.Link will always return some string when you have a $imageLinkInternalURL Sitetree object related in the has_one.

    One solution would be to test if the current Object has a $ImageLinkInternalURL Object related by checking the ID of it, $ImageLinkInternalURLID.

    <div class="halfColumn">
       <% if $ImageLinkInternalURLID %>
         <%-- we have a has_one, use this --%>
         <a href="$ImageLinkInternalURL.Link">
             <p>Internal Link</p>
         </a>
        <% else_if $ImageExternalLink %>
            <%-- we have an external link defined --%>
            <a href="$ImageExternalLink">
                <p>External Link</p>
            </a>
        <% else %>
            <%-- fallback --%>
            <img class="dropShadow" src="$Image.URL" style="max-height: 220px;">
        <% end_if %>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大