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.

Archive /

Our old forums are still available as a read-only archive.

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

Check if image exists in template


Reply


3 Posts   2152 Views

Avatar
Fredrik

Community Member, 1 Post

7 September 2008 at 8:10am

Hi, i'm new to PHP and Silverstripe, but using it to build a site right now and so far i like the flexibility alot. Question:
I have extended my page to include an ImageField (followed the tutorial) and i want to use the image as a banner on the page if there is an image attached. How do i check if an image exists in the template? Sort of like this:

<% if BannerImage %>
Render html specific if bannerimage exists
<% else %>
Render html for no image
<% end if %>

Avatar
grilldan

Community Member, 135 Posts

7 September 2008 at 9:40am

Edited: 07/09/2008 9:43am

Hmmm. You could try this:

1) Make a Page called "bannerimage" with the html for the banner.
2) use this in your page.ss file

<% if Page(bannerimage) %>
$Content
<% else %>
render html for no img
<% end_if %>

This might work, it might not work, I'm not sure.

**edit**
If that doesnt work, try this:

<% if Page(bannerimage) %>
<% control Page(bannerimage) %>
$Content
<% end_control %>
<% else %>
render html for no img
<% end_if %>

**edit**
Don't forget to go to the behavior tab of the "bannerimage" page, and turn off "show in menus" and "show in searches"

Avatar
Willr

Forum Moderator, 5513 Posts

7 September 2008 at 11:29am

<% if BannerImage %>
Render html specific if bannerimage exists
<% else %>
Render html for no image
<% end if %>

Should work fine!