Faloude Faloude - 1 month ago 6x
PHP Question

SilverStripe template statement returns false positive

I've been at this for hours. I've got two pages types:

Page extends SiteTree
which holds no additional
fields and I have a
SubPage extends Page
which holds

This is the if statement in the Page.ss template:

<% if $Banner %>
<% include Banner %> <- include the banner .ss template
<% end_if %>

For some reason this returns true even if there is no
field named Banner in the current PageType. However, there is a Table in the database named Banner as well, and it seems that the existence of this table triggers $Banner.

Is there a way to fix this by making the template clear that $Banner refers to a $db field, and if this $db field is not declared for this pagetype, then return false.


The way to be 100% sure is to create a function in the controller of the SubPage called like BannerExists and then use that in the if.

public function BannerExists() {
    return !empty($this->Banner);

This is because Silverstripe is returning an empty string where the field isn't found.

Alternatively you might use...

<% if not $Banner == "" %>
   <% include Banner %>
<% end_if %>


<% if $Banner == "" %><% else %>
   <% include Banner %>
<% end_if %>