Jump to:

23355 Posts in 17734 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Checking variables of a chlid

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: 148 Views
  • Bagzli
    Avatar
    Community Member
    26 Posts

    Checking variables of a chlid Link to this post

    Hello,

    I am trying to achieve few things. I have a TeamHolder Page and then children of TeamHolder are TeamPlayer. I would like to write a method in TeamHolder that is going to check if TeamPlayer has a facebook field filled out, if it does it will display the facebook icon.

    Here is what I have:

    <ul id="team-list">
    <% loop $Children %>
    <li>
    <div class="player">
    <div class="player-left">
    <div class="player-image">
    <a href="$Link" ><img src="$PlayerPhoto.Link" alt="/" /></a>
    </div>
    <div class="player-social">
    <% if MySocialClass() %>
    <ul class="social4">
    <% else %>
    <ul class="social1">
    <% end_if %>
    <% if $Facebook %>
    <li><a title="Facebook" href="$Facebook" target="_blank">Facebook</a></li>
    <% else %>
    <li></li>
    <% end_if %>
    </ul>
    </div>
    </div>
    </div>
    </li>
    <% end_loop %>
    </ul>

    In my TeamHolder.php I have

    class TeamHolder_Controller extends Page_Controller{
    public function MySocialClass(){
    return "social4";
    }
    }

    For some reason my ul has class of social1 instead of social4 and I can't figure out why.

    The second portion to this problem is, how would I check in MySocialClass() if the child I am currently on has facebook filled out?

    I am guessing that I have to pass the child through parameters somehow and then check the $Facebook value, but how do I do this in silverstripe?

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Checking variables of a chlid Link to this post

    In your template you're calling the MySocialClass() function from within the Children loop. At that point you are already within the context of the TeamPlayer page, so the function in the TeamHolder_Controller will not be called unless you add Top to it, like this:

    <% if $Top.MySocialClass %>

    But far easier to ask the TeamPlayer page directly, as you're inthat context. So if MySocialClass is already datafield in TeamPlayer, you can just:

    <% if $MySocialClass %>

    (Skip the parenthesis)

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