Jump to:

23491 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Display data in template conditionally on field values

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: 638 Views
  • bostonmark
    Avatar
    Community Member
    10 Posts

    Display data in template conditionally on field values Link to this post

    I have two fields in the back-end, an image field and a text area for YouTube embed code. I want to show the image field in the template only if the textarea has no value. I'm new to SS; my approach for this was to create a function in my page class to return a bool if the textrea has a value. On the front-end template I will access that bool in a conditional block like so:

    <% if hasEmbedCode %>
    $VideoEmbedCode
    <% else %>
    $PlaceholderImage
    <% end_if %>

    I'm not sure how to write the hasEmbedCode() function to return a bool based on the value of the Embed Code field.

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: Display data in template conditionally on field values Link to this post

    Hello, you don't need to write a new function ; this code should work :

    <% if VideoEmbedCode %>
    $VideoEmbedCode
    <% else %>
    $PlaceholderImage
    <% end_if %>


    Stan

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