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

Checking variables of a chlid


Go to End


2 Posts   662 Views

Avatar
Bagzli

Community Member, 71 Posts

3 May 2014 at 11:06am

Edited: 03/05/2014 11:09am

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

4 May 2014 at 8:57pm

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)