Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » if page control

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: 1584 Views
  • sonicparke
    Avatar
    74 Posts

    if page control Link to this post

    I added a custom tab to the Page page type. I've got it working using an include in Page.ss. 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?

  • Re: if page control Link to this post

    Perhaps you could try something like:

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

  • sonicparke
    Avatar
    74 Posts

    Re: if page control Link to this post

    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 Announcement.ss and put in in the includes folder of my theme.

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

    3. I added the include to the appropriate place in Page.ss

    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 Announcement.ss 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
    Avatar
    Community Member
    71 Posts

    Re: if page control Link to this post

    sonicparke-

    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!

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