Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Function not displaying result in my template

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1441 Views
  • ccburns
    Avatar
    Community Member
    74 Posts

    Function not displaying result in my template Link to this post

    Hi All,

    Before I get go and try to work out a work around for this issue I thought I would see if I am doing something very wrong which is causing the problem. Sorry if this is stupidly simple....

    Okay, firstly I have used ModelAdmin to create a product list (this is just an online catalogue not e-commerce). I have create a page type called ProductDetails.php

    <?php

    /**
    * Application Aggregator to display the list of Markets in the system
    */

    class ProductDetails extends Page {
       
    }

    class ProductDetails_Controller extends Page_Controller {
       public static $allowed_actions = array (
          'show'
       );
       function ProductDetails() {
          $where = Convert::raw2SQL(Director::urlParam('ID'));
          $whereStatement = "ID = '". $where ."'";

          return DataObject::get("Product", $whereStatement);
       }
       function ImageExists() {
          // Check that the image file exists for this page
          // in the specified path
          $filename = 'assets/Uploads/products/product-$ItemId-1.png';
          if(Director::fileExists($filename) == true) {
             return true;
          }else{
             return false;
          }
       }
    }

    So the ImageExists() function should check if the particular image is on the file system and if it is it should display the image using an if statement in the template

    <% if ImageExists %><img src="assets/Uploads/products/product-$ItemId-1.png" alt="$Title" /><% end_if %>

    Now the problem is that this needs to exist inside the <% control ProductDetails %> object. Everytime I test this nothing work. I have even tried just returning a string and outputting that directly within this controller. If I put the <% if ImageExists %> outside the Product Details controller then it shows up fine...

    Below is my full template. If you have any suggestions of what I should be trying I would be truly grateful. It has had me puzzled for the last couple of days.

    Kind Regards,
    Colin

          <div id="feature-container">
             <% include SearchFormHTML %>
             <div id="feature-banner">
                <img src="/$themeDir/images/feature-image-1.jpg" alt="Feature Image" />
             </div>
          </div>
          <% include BreadCrumb %>   
          <% include LeftNavigationProducts %>
          <div id="right-container" class="typography">
             <% if ProductDetails %>
                <% control ProductDetails %>
                   <div id="ProductDetails">
                      <h3>Product Information</h3>
                      <h4>Item Name: <span class="normal">$ItemName</span></h4>
                      <h4>Item ID: <span class="normal">$ItemId</span></h4>
                      <div class="product-indent">$Description</div>
                      <h4>Benefits</h4>
                      <ul class="product-ul-indent benefits">
                         <li>$Benefits</li>
                      </ul>
                      <h4>Features</h4>
                      <ul class="product-ul-indent features">
                         <li>$Features</li>
                      </ul>
                      <h4>Specifications</h4>
                      <ul class="product-ul-indent specifications">
                         <% if PackageSize %><li><strong>Package Size:</strong> $PackageSize</li><% end_if %>
                         <% if PackagingGroupId %><li><strong>Packaging Group ID:</strong> $PackagingGroupId</li><% end_if %>
                         <% if PurchaseModel %><li><strong>Purchase Model:</strong> $PurchaseModel</li><% end_if %>
                         <% if RecId %><li><strong>Rec Id:</strong> $RecId</li><% end_if %>
                         <% if ReqGroupId %><li><strong>ReqGroupId:</strong> $ReqGroupId</li><% end_if %>
                         <% if Markets %><li><strong>Markets:</strong> $Markets</li><% end_if %>
                         <% if Applications %><li><strong>Applications:</strong> $Applications</li><% end_if %>
                         <% if Acronyms %><li><strong>Acronyms:</strong> $Acronyms</li><% end_if %>
                         <% if Oem %><li><strong>Vendor:</strong> $Oem</li><% end_if %>
                         <% if Division %><li><strong>Division:</strong> $Division</li><% end_if %>
                      </ul>
                      <p class="product-cost"><% if DisplayPriceOnWeb == Yes %><span class="product-cost-label">COST</span> $$Price Inc GST<% end_if %><a href="/" class="enquire-now"></a></p>
                   </div>
                   <div id="ProductExtras">
                      <!--
                      This is where I want to check if the image exists on the file system and if it doesn't show a default image
                       -->
                       <% if ImageExists %>
                          <!-- SHOW ACTUAL IMAGE -->
                         <img src="/assets/Uploads/products/product-$ItemId-1.jpg" alt="$ItemName" />
                      <% else %>
                         <!-- SHOW DEFAULT IMAGE -->
                         <img src="/assets/Uploads/products/default.jpg" alt="$ItemName" />
                      <% end_if %>
                      <br/>
                      <!-- WILL BE REPEATED FOR ALL THESE IMAGES AS WELL ONCE I WORK IT OUT-->
                      <img src="/assets/Uploads/products/product-$ItemId-2.jpg" alt="$ItemName" />
                      <img src="/assets/Uploads/products/product-$ItemId-3.jpg" alt="$ItemName" />
                      <img src="/assets/Uploads/products/product-$ItemId-4.jpg" alt="$ItemName" />
                      <img src="/assets/Uploads/products/product-$ItemId-5.jpg" alt="$ItemName" />
                      <p>Roll over thumbnails for larger image.</p>
                   </div>
                <% end_control %>
             <% else %>
                <p class="TryOurSearch"><strong>Sorry there are no products in this category.</strong><br/><br/>Why don't you try our search function to help find what you are looking for.</p>
             <% end_if %>
          </div>

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Function not displaying result in my template Link to this post

    Its because as soon as you go into that <% control ProductDetails %> the scope the the template changes to the 'Product' object. To call that ImageExists method you either need to move it from the ProductDetails_Controller to the Product class or you could try doing <% if Top.ImageExists %> in the control - the Top goes back up to the top most level.

  • ccburns
    Avatar
    Community Member
    74 Posts

    Re: Function not displaying result in my template Link to this post

    Thanks Willr,

    I'll give those two things and try and update the post once tested.

    Cheers,
    Colin

    1441 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.