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

Display data in template conditionally on field values


Go to End


2 Posts   1417 Views

Avatar
bostonmark

Community Member, 10 Posts

5 September 2011 at 8:08am

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.

Avatar
stallain

Community Member, 68 Posts

5 September 2011 at 10:28am

Edited: 05/09/2011 10:33am

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

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

Stan