Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Function not displaying result in my template


Go to End


3 Posts   2015 Views

Avatar
ccburns

Community Member, 79 Posts

11 January 2010 at 1:58pm

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>

Avatar
Willr

Forum Moderator, 5523 Posts

11 January 2010 at 3:18pm

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.

Avatar
ccburns

Community Member, 79 Posts

11 January 2010 at 3:30pm

Thanks Willr,

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

Cheers,
Colin