Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Check if image exists in template

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

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

Page: 1
Go to End
Author Topic: 2111 Views
  • Fredrik
    Avatar
    Community Member
    1 Post

    Check if image exists in template Link to this post

    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 %>

  • grilldan
    Avatar
    Community Member
    135 Posts

    Re: Check if image exists in template Link to this post

    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"

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Check if image exists in template Link to this post

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

    Should work fine!

    2111 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.