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, Ed, biapar, Willr, Ingo

if page control

Go to End

4 Posts   1758 Views


74 Posts

11 July 2008 at 5:27pm

I added a custom tab to the Page page type. I've got it working using an include in How can I tell it only to show up if there is text in the text field on that tab and if there's not I don't want it to be included at all?

Grayzag (aka ajshort)

29 Posts

11 July 2008 at 7:24pm

Edited: 11/07/2008 7:25pm

Perhaps you could try something like:

<% if SomeField %>
    <% include SomeFile %>
<% end_if %>


74 Posts

11 July 2008 at 8:33pm

Edited: 11/07/2008 8:38pm

That did it! Well I had to tweak it a bit but that was what I needed.

Here's what I did so it's documented in case someone else needs to know:

1. I added an "Announcement" tab to Page.php.

class Page extends SiteTree {
    static $db = array(
        "AnnouncementContent" => "Text",
           function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Content.Announcement', new TextField('AnnouncementContent'));
        return $fields;
    static $has_one = array(

2. I created and put in in the includes folder of my theme.

<% control Page(home) %>
<% if AnnouncementContent %>
    <div id="announcement">
<% end_if %>
<% end_control %>

3. I added the include to the appropriate place in

Now What would make it better is to be able to remove the tab from all the pages other than Home. But do that without creating a HomePage page type. Home is just a Page page type like all the rest. With the page control in it will only take the text that's entered in the Announcement tab on the home page. But it would make it less confusing for the user if that tab was only there on the page that it pulls from.

BLU42 Media

Community Member, 71 Posts

13 July 2008 at 3:37pm


You're really close! You could try adding an if statement around your addFieldToTab method:

if ('Home' == $this->Title) {
     $fields->addFieldToTab( 'Root.Content.Announcement', new TextField( 'AnnouncementContent' ) );
return $fields;

Depending on what you need, you could also compare that to MenuTitle instead of Title.

Hope that helps!